/** * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CODE_FIX_PROVIDER_H #define CODE_FIX_PROVIDER_H #include #include #include #include #include "services/text_change/change_tracker.h" #include "code_fixes/code_fix_types.h" #include "es2panda.h" namespace ark::es2panda::lsp { class CodeFixProvider { private: std::unordered_map> errorCodeToFixes_; std::unordered_map> fixIdToRegistration_; public: std::unordered_map> GetErrorCodeToFixes() const { return errorCodeToFixes_; } std::unordered_map> GetFixIdToRegistration() const { return fixIdToRegistration_; } void RegisterCodeFix(const std::string &aliasName, std::unique_ptr registration); static CodeFixProvider &Instance(); std::string FormatWithArgs(const std::string &text); std::string DiagnosticToString(const DiagnosticAndArguments &diag); CodeFixAction CreateCodeFixActionWorker(std::string &fixName, std::string &description, std::vector &changes, std::string &fixId, std::string &fixAllDescription, std::vector command); CodeFixAction CreateCodeFixActionWithoutFixAll(std::string &fixName, std::vector &changes, DiagnosticAndArguments &description); CodeFixAction CreateCodeFixAction(std::string fixName, std::vector changes, DiagnosticAndArguments &description, std::string fixId, DiagnosticAndArguments &fixAllDescription, std::vector &command); std::string GetFileName(const std::string &filePath); std::vector GetSupportedErrorCodes(); DiagnosticReferences *GetDiagnostics(const CodeFixContextBase &context); bool ShouldIncludeFixAll(const CodeFixRegistration ®istration, const std::vector &diagnostics); std::vector GetFixes(const CodeFixContext &context); void EachDiagnostic(const CodeFixAllContext &context, const std::vector &errorCodes, const std::function &cb); CombinedCodeActions CodeFixAll(const CodeFixAllContext &context, const std::vector &errorCodes, std::function use); FileTextChanges CreateFileTextChanges(const std::string &fileName, const std::vector &textChanges); CombinedCodeActions GetAllFixes(const CodeFixAllContext &context); }; template struct AutoCodeFixRegister { constexpr explicit AutoCodeFixRegister(const std::string &name, std::unique_ptr registration) { CodeFixProvider::Instance().RegisterCodeFix(name, std::move(registration)); } constexpr explicit AutoCodeFixRegister(const std::string &name) : AutoCodeFixRegister(name, std::make_unique()) { } }; } // namespace ark::es2panda::lsp #endif