1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_LSP_INCLUDE_API_H 17 #define ES2PANDA_LSP_INCLUDE_API_H 18 19 // Switch off the linter for C header 20 // NOLINTBEGIN 21 // 22 23 #include <stddef.h> 24 #include <cstddef> 25 #include <string> 26 #include <variant> 27 #include <vector> 28 #include "line_column_offset.h" 29 #include "public/es2panda_lib.h" 30 #include "cancellation_token.h" 31 #include "user_preferences.h" 32 #include "class_hierarchies.h" 33 #include "find_references.h" 34 #include "find_rename_locations.h" 35 #include "class_hierarchy_info.h" 36 #include "completions.h" 37 #include "refactors/refactor_types.h" 38 #include "applicable_refactors.h" 39 #include "todo_comments.h" 40 #include "types.h" 41 #include "formatting/formatting_settings.h" 42 #include "user_preferences.h" 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 typedef struct SafeDeleteLocation { 49 std::string uri; 50 size_t start; 51 size_t length; 52 } SafeDeleteLocation; 53 54 typedef struct DefinitionInfo { 55 DefinitionInfo() = default; DefinitionInfoDefinitionInfo56 DefinitionInfo(std::string f, size_t s, size_t l) : fileName(f), start(s), length(l) {} 57 std::string fileName; 58 size_t start; 59 size_t length; 60 } DefinitionInfo; 61 62 typedef struct ReferenceInfo { 63 ReferenceInfo() = default; ReferenceInfoReferenceInfo64 ReferenceInfo(std::string f, size_t s, size_t l) : fileName(f), start(s), length(l) {} 65 std::string fileName; 66 size_t start; 67 size_t length; 68 } ReferenceInfo; 69 70 typedef struct References { 71 std::vector<ReferenceInfo> referenceInfos; 72 } References; 73 74 typedef struct Position { 75 size_t line_; // Line number 76 size_t character_; // Character position in the line 77 line_Position78 Position(unsigned int line_num = 0, unsigned int character_pos = 0) : line_(line_num), character_(character_pos) {} 79 } Position; 80 81 typedef struct Range { 82 Position start; // Start position 83 Position end; // End position 84 startRange85 Range(Position start_pos = Position(), Position end_pos = Position()) : start(start_pos), end(end_pos) {} 86 } Range; 87 88 typedef struct Location { 89 std::string uri_; // The URI of the document 90 Range range_; // The range of the diagnostic in the document uri_Location91 Location(std::string uri = "", const Range range = Range()) : uri_(uri), range_(range) {} 92 } Location; 93 94 enum class DiagnosticSeverity { Error = 1, Warning = 2, Information = 3, Hint = 4 }; 95 96 enum class DiagnosticTag { Unnecessary = 1, Deprecated = 2 }; 97 98 typedef struct CodeDescription { 99 std::string href_; href_CodeDescription100 CodeDescription(std::string href = "") : href_(href) {} 101 } CodeDescription; 102 103 typedef struct DiagnosticRelatedInformation { 104 Location location_; 105 std::string message_; 106 107 DiagnosticRelatedInformation(const Location location = Location(), const std::string message = "") location_DiagnosticRelatedInformation108 : location_(location), message_(message) 109 { 110 } 111 } DiagnosticRelatedInformation; 112 113 typedef struct Diagnostic { 114 Range range_; // The range at which the message applies. 115 DiagnosticSeverity severity_; // The diagnostic's severity. 116 std::variant<int, std::string> code_; // The diagnostic's code. 117 CodeDescription codeDescription_; // The error code description. 118 std::string source_; // The source of the diagnostic. 119 std::string message_; // The diagnostic's message. 120 std::vector<DiagnosticTag> tags_; // Additional metadata about the diagnostic. 121 std::vector<DiagnosticRelatedInformation> relatedInformation_; // Related diagnostics. 122 std::variant<int, std::string> data_; // Additional data. 123 124 Diagnostic(const Range range, const std::vector<DiagnosticTag> tags, 125 const std::vector<DiagnosticRelatedInformation> relatedInformation, 126 DiagnosticSeverity severity = DiagnosticSeverity::Warning, 127 const std::variant<int, std::string> code = 100, std::string message = "default message", 128 const CodeDescription codeDescription = {}, std::string source = "default source", 129 const std::variant<int, std::string> data = {}) range_Diagnostic130 : range_(range), 131 severity_(severity), 132 code_(code), 133 codeDescription_(codeDescription), 134 source_(source), 135 message_(message), 136 tags_(tags), 137 relatedInformation_(relatedInformation), 138 data_(data) 139 { 140 } 141 } Diagnostic; 142 143 typedef struct DiagnosticReferences { 144 std::vector<Diagnostic> diagnostic; 145 } DiagnosticReferences; 146 147 enum class CommentKind { SINGLE_LINE, MULTI_LINE }; 148 149 typedef struct CommentRange { CommentRangeCommentRange150 CommentRange() {} CommentRangeCommentRange151 CommentRange(size_t p, size_t e, CommentKind k) : pos_(p), end_(e), kind_(k) {} 152 size_t pos_; 153 size_t end_; 154 CommentKind kind_; 155 } CommentRange; 156 157 enum class AccessKind { READ, WRITE, READWRITE }; 158 159 typedef struct ReferenceLocation { 160 std::string uri; 161 size_t start; // Start position 162 size_t end; // End position 163 bool isDefinition; 164 AccessKind accessKind; 165 bool isImport; 166 } ReferenceLocation; 167 168 typedef struct FileNodeInfo { 169 std::string tokenName; 170 std::string tokenId; FileNodeInfoFileNodeInfo171 FileNodeInfo(const std::string &token, const std::string &id) : tokenName(token), tokenId(id) {} 172 } FileNodeInfo; 173 174 typedef struct ReferenceLocationList { 175 std::vector<ReferenceLocation> referenceLocation; 176 } ReferenceLocationList; 177 178 enum class HighlightSpanKind { NONE, DEFINITION, REFERENCE, WRITTEN_REFERENCE }; 179 180 typedef struct HighlightSpan { 181 std::string fileName_; 182 bool isInString_; 183 TextSpan textSpan_; 184 TextSpan contextSpan_; 185 HighlightSpanKind kind_; 186 HighlightSpan(std::string fileName = "fileName", bool isInString = false, TextSpan textSpan = {0, 0}, 187 TextSpan contextSpan = {0, 0}, HighlightSpanKind kind = HighlightSpanKind::NONE) fileName_HighlightSpan188 : fileName_(fileName), isInString_(isInString), textSpan_(textSpan), contextSpan_(contextSpan), kind_(kind) 189 { 190 } 191 } HighlightSpan; 192 193 typedef struct DocumentHighlights { 194 std::string fileName_; 195 std::vector<HighlightSpan> highlightSpans_; 196 DocumentHighlights(std::string fileName = "fileName", std::vector<HighlightSpan> highlightSpans = {}) fileName_DocumentHighlights197 : fileName_(fileName), highlightSpans_(highlightSpans) 198 { 199 } 200 } DocumentHighlights; 201 202 struct FieldListProperty { 203 std::string kind; 204 std::optional<std::vector<std::string>> modifierKinds; 205 std::string displayName; 206 size_t start; 207 size_t end; 208 FieldListPropertyFieldListProperty209 FieldListProperty(std::string k, std::optional<std::vector<std::string>> m, std::string d, size_t s, size_t e) 210 : kind(std::move(k)), modifierKinds(std::move(m)), displayName(std::move(d)), start(s), end(e) 211 { 212 } 213 }; 214 215 struct FieldsInfo { 216 std::string name; 217 std::vector<FieldListProperty> properties; 218 bool operator<(const FieldsInfo &other) const 219 { 220 return name < other.name; 221 } 222 FieldsInfo() = default; FieldsInfoFieldsInfo223 FieldsInfo(const FieldsInfo &fi) : name(fi.name), properties(fi.properties) {} 224 }; 225 226 struct LspClassPropertyInfo { 227 FieldsInfo fieldsInfo; LspClassPropertyInfoLspClassPropertyInfo228 LspClassPropertyInfo(FieldsInfo f) : fieldsInfo(std::move(f)) {} 229 }; 230 231 typedef struct DocumentHighlightsReferences { 232 std::vector<DocumentHighlights> documentHighlights_; 233 } DocumentHighlightsReferences; 234 235 struct DocTagInfo { 236 private: 237 std::string name_; 238 std::string text_; 239 240 public: 241 explicit DocTagInfo(std::string name = "", std::string text = "") : name_ {std::move(name)}, text_ {std::move(text)} 242 { 243 } 244 GetNameDocTagInfo245 std::string GetName() const 246 { 247 return name_; 248 } GetTextDocTagInfo249 std::string GetText() const 250 { 251 return text_; 252 } 253 254 bool operator==(const DocTagInfo &other) const 255 { 256 return name_ == other.name_ && text_ == other.text_; 257 } 258 bool operator!=(const DocTagInfo &other) const 259 { 260 return !(*this == other); 261 } 262 }; 263 264 struct QuickInfo { 265 private: 266 std::string kind_; 267 std::string kindModifiers_; 268 TextSpan textSpan_; 269 std::vector<SymbolDisplayPart> displayParts_; 270 std::vector<SymbolDisplayPart> document_; 271 std::vector<DocTagInfo> tags_; 272 std::string fileName_; 273 274 public: 275 explicit QuickInfo(std::string kind = "", std::string kindModifiers = "", TextSpan span = TextSpan(0, 0), 276 std::vector<SymbolDisplayPart> displayParts = {}, std::vector<SymbolDisplayPart> document = {}, 277 std::vector<DocTagInfo> tags = {}, std::string fileName = "") 278 : kind_ {std::move(kind)}, 279 kindModifiers_ {std::move(kindModifiers)}, 280 textSpan_ {span}, 281 displayParts_ {std::move(displayParts)}, 282 document_ {std::move(document)}, 283 tags_ {std::move(tags)}, 284 fileName_ {std::move(fileName)} 285 { 286 } 287 GetKindQuickInfo288 std::string GetKind() const 289 { 290 return kind_; 291 } GetKindModifiersQuickInfo292 std::string GetKindModifiers() const 293 { 294 return kindModifiers_; 295 } GetTextSpanQuickInfo296 TextSpan GetTextSpan() const 297 { 298 return textSpan_; 299 } GetDisplayPartsQuickInfo300 std::vector<SymbolDisplayPart> GetDisplayParts() const 301 { 302 return displayParts_; 303 } GetDocumentQuickInfo304 std::vector<SymbolDisplayPart> GetDocument() const 305 { 306 return document_; 307 } GetTagsQuickInfo308 std::vector<DocTagInfo> GetTags() const 309 { 310 return tags_; 311 } GetFileNameQuickInfo312 std::string GetFileName() const 313 { 314 return fileName_; 315 } 316 317 bool operator==(const QuickInfo &other) const 318 { 319 return kind_ == other.kind_ && kindModifiers_ == other.kindModifiers_ && textSpan_ == other.textSpan_ && 320 displayParts_ == other.displayParts_ && document_ == other.document_ && tags_ == other.tags_ && 321 fileName_ == other.fileName_; 322 } 323 bool operator!=(const QuickInfo &other) const 324 { 325 return !(*this == other); 326 } 327 }; 328 329 struct CompletionEntryDetails { 330 private: 331 std::string name_; 332 std::string kind_; 333 std::string kindModifiers_; 334 std::vector<SymbolDisplayPart> displayParts_; 335 std::vector<SymbolDisplayPart> document_; 336 std::vector<SymbolDisplayPart> source_; 337 std::vector<SymbolDisplayPart> sourceDisplay_; 338 std::string fileName_; 339 340 public: 341 explicit CompletionEntryDetails(std::string name = "", std::string kind = "", std::string kindModifiers = "", 342 std::vector<SymbolDisplayPart> displayParts = {}, 343 std::vector<SymbolDisplayPart> document = {}, 344 std::vector<SymbolDisplayPart> source = {}, 345 std::vector<SymbolDisplayPart> sourceDisplay = {}, std::string fileName = "") 346 : name_ {std::move(name)}, 347 kind_ {std::move(kind)}, 348 kindModifiers_ {std::move(kindModifiers)}, 349 displayParts_ {std::move(displayParts)}, 350 document_ {std::move(document)}, 351 source_ {std::move(source)}, 352 sourceDisplay_ {std::move(sourceDisplay)}, 353 fileName_ {std::move(fileName)} 354 { 355 } 356 GetNameCompletionEntryDetails357 std::string GetName() const 358 { 359 return name_; 360 } GetKindCompletionEntryDetails361 std::string GetKind() const 362 { 363 return kind_; 364 } GetKindModifiersCompletionEntryDetails365 std::string GetKindModifiers() const 366 { 367 return kindModifiers_; 368 } GetDisplayPartsCompletionEntryDetails369 std::vector<SymbolDisplayPart> GetDisplayParts() const 370 { 371 return displayParts_; 372 } GetDocumentCompletionEntryDetails373 std::vector<SymbolDisplayPart> GetDocument() const 374 { 375 return document_; 376 } GetSourceCompletionEntryDetails377 std::vector<SymbolDisplayPart> GetSource() const 378 { 379 return source_; 380 } GetSourceDisplayCompletionEntryDetails381 std::vector<SymbolDisplayPart> GetSourceDisplay() const 382 { 383 return sourceDisplay_; 384 } GetFileNameCompletionEntryDetails385 std::string GetFileName() const 386 { 387 return fileName_; 388 } 389 390 bool operator==(const CompletionEntryDetails &other) const 391 { 392 return name_ == other.name_ && kind_ == other.kind_ && kindModifiers_ == other.kindModifiers_ && 393 displayParts_ == other.displayParts_ && document_ == other.document_ && source_ == other.source_ && 394 sourceDisplay_ == other.sourceDisplay_ && fileName_ == other.fileName_; 395 } 396 bool operator!=(const CompletionEntryDetails &other) const 397 { 398 return !(*this == other); 399 } 400 }; 401 402 typedef struct FileDiagnostic { 403 es2panda_AstNode *node; 404 Diagnostic diagnostic; 405 FileDiagnosticFileDiagnostic406 FileDiagnostic(es2panda_AstNode *n, const Diagnostic &diag, Position start, Position end) 407 : node(n), 408 diagnostic(Diagnostic(Range(start, end), diag.tags_, diag.relatedInformation_, diag.severity_, diag.code_, 409 diag.message_, diag.codeDescription_, diag.source_, diag.data_)) 410 { 411 } 412 } FileDiagnostic; 413 414 typedef struct DeclInfo { 415 std::string fileName; 416 std::string fileText; 417 } DeclInfo; 418 419 enum class HierarchyType { OTHERS, INTERFACE, CLASS }; 420 421 struct TypeHierarchies { 422 TypeHierarchies() = default; TypeHierarchiesTypeHierarchies423 TypeHierarchies(std::string f, std::string n, HierarchyType t, size_t p) 424 : fileName(std::move(f)), name(std::move(n)), type(t), pos(p) 425 { 426 } 427 bool operator==(const TypeHierarchies &other) const 428 { 429 return fileName == other.fileName && name == other.name && type == other.type && pos == other.pos; 430 } 431 bool operator!=(const TypeHierarchies &other) const 432 { 433 return !(*this == other); 434 } 435 bool operator<(const TypeHierarchies &other) const 436 { 437 return std::tie(fileName, name, type, pos) < std::tie(other.fileName, other.name, other.type, other.pos); 438 } 439 std::string fileName; 440 std::string name; 441 HierarchyType type = HierarchyType::OTHERS; 442 size_t pos = 0; 443 std::vector<TypeHierarchies> subOrSuper; 444 }; 445 446 struct TypeHierarchiesInfo { 447 TypeHierarchiesInfo() = default; TypeHierarchiesInfoTypeHierarchiesInfo448 TypeHierarchiesInfo(std::string f, std::string n, HierarchyType t, size_t p) 449 : fileName(std::move(f)), name(std::move(n)), type(t), pos(p) 450 { 451 } 452 std::string fileName; 453 std::string name; 454 HierarchyType type = HierarchyType::OTHERS; 455 size_t pos = 0; 456 TypeHierarchies superHierarchies; 457 TypeHierarchies subHierarchies; 458 }; 459 460 struct InstallPackageActionInfo { 461 std::string type_; 462 std::optional<std::string> file; 463 std::optional<std::string> packageName; 464 }; 465 466 struct CodeActionInfo { 467 std::string description_; 468 std::vector<FileTextChanges> changes_; 469 std::vector<InstallPackageActionInfo> commands_; 470 }; 471 472 struct CombinedCodeActionsInfo { 473 std::vector<FileTextChanges> changes_; 474 std::vector<InstallPackageActionInfo> commands_; 475 }; 476 477 struct CodeFixActionInfo : CodeActionInfo { 478 std::string fixName_; 479 std::string fixId_ = {}; 480 std::string fixAllDescription_ = {}; 481 }; 482 483 struct CodeFixActionInfoList { 484 std::vector<CodeFixActionInfo> infos_; 485 }; 486 487 struct CodeFixOptions { 488 ark::es2panda::lsp::CancellationToken token; 489 ark::es2panda::lsp::FormatCodeSettings options; 490 ark::es2panda::lsp::UserPreferences preferences; 491 }; 492 493 typedef struct LSPAPI { 494 DefinitionInfo (*getDefinitionAtPosition)(es2panda_Context *context, size_t position); 495 std::vector<ark::es2panda::lsp::ApplicableRefactorInfo> (*getApplicableRefactors)(es2panda_Context *context, 496 const char *kind, 497 size_t position); 498 DefinitionInfo (*getImplementationAtPosition)(es2panda_Context *context, size_t position); 499 bool (*isPackageModule)(es2panda_Context *context); 500 ark::es2panda::lsp::CompletionEntryKind (*getAliasScriptElementKind)(es2panda_Context *context, size_t position); 501 References (*getFileReferences)(char const *fileName, es2panda_Context *context, bool isPackageModule); 502 DeclInfo (*getDeclInfo)(es2panda_Context *context, size_t position); 503 std::vector<ark::es2panda::lsp::ClassHierarchyItemInfo> (*getClassHierarchiesImpl)(es2panda_Context *context, 504 const char *fileName, 505 size_t pos); 506 bool (*getSafeDeleteInfo)(es2panda_Context *context, size_t position); 507 References (*getReferencesAtPosition)(es2panda_Context *context, DeclInfo *declInfo); 508 es2panda_AstNode *(*getPrecedingToken)(es2panda_Context *context, const size_t pos); 509 std::string (*getCurrentTokenValue)(es2panda_Context *context, size_t position); 510 std::vector<FileTextChanges> (*OrganizeImportsImpl)(es2panda_Context *context, char const *fileName); 511 QuickInfo (*getQuickInfoAtPosition)(const char *fileName, es2panda_Context *context, size_t position); 512 CompletionEntryDetails (*getCompletionEntryDetails)(const char *entryName, const char *fileName, 513 es2panda_Context *context, size_t position); 514 TextSpan (*getSpanOfEnclosingComment)(es2panda_Context *context, size_t pos, bool onlyMultiLine); 515 DiagnosticReferences (*getSemanticDiagnostics)(es2panda_Context *context); 516 DiagnosticReferences (*getSyntacticDiagnostics)(es2panda_Context *context); 517 DiagnosticReferences (*getCompilerOptionsDiagnostics)(char const *fileName, 518 ark::es2panda::lsp::CancellationToken cancellationToken); 519 TypeHierarchiesInfo (*getTypeHierarchies)(es2panda_Context *searchContext, es2panda_Context *context, 520 size_t position); 521 DocumentHighlightsReferences (*getDocumentHighlights)(es2panda_Context *context, size_t position); 522 std::vector<ark::es2panda::lsp::RenameLocation> (*findRenameLocations)( 523 const std::vector<ark::es2panda::SourceFile> &files, const ark::es2panda::SourceFile &file, size_t position); 524 std::vector<ark::es2panda::lsp::RenameLocation> (*findRenameLocationsWithCancellationToken)( 525 ark::es2panda::lsp::CancellationToken *tkn, const std::vector<ark::es2panda::SourceFile> &files, 526 const ark::es2panda::SourceFile &file, size_t position); 527 std::vector<SafeDeleteLocation> (*FindSafeDeleteLocation)(es2panda_Context *ctx, 528 const std::tuple<std::string, std::string> *declInfo); 529 std::vector<ark::es2panda::lsp::ReferencedNode> (*findReferences)( 530 ark::es2panda::lsp::CancellationToken *tkn, const std::vector<ark::es2panda::SourceFile> &srcFiles, 531 const ark::es2panda::SourceFile &srcFile, size_t position); 532 std::vector<FieldsInfo> (*getClassPropertyInfo)(es2panda_Context *context, size_t pos, bool shouldCollectInherited); 533 DiagnosticReferences (*getSuggestionDiagnostics)(es2panda_Context *context); 534 ark::es2panda::lsp::CompletionInfo (*getCompletionsAtPosition)(es2panda_Context *context, size_t position); 535 ark::es2panda::lsp::ClassHierarchy (*getClassHierarchyInfo)(es2panda_Context *context, size_t position); 536 std::vector<TextSpan> (*getBraceMatchingAtPosition)(char const *fileName, size_t position); 537 ark::es2panda::lsp::RefactorEditInfo (*getClassConstructorInfo)(es2panda_Context *context, size_t position, 538 const std::vector<std::string> &properties); 539 std::vector<Location> (*getImplementationLocationAtPosition)(es2panda_Context *context, int position); 540 ark::es2panda::lsp::LineAndCharacter (*toLineColumnOffset)(es2panda_Context *context, size_t position); 541 std::vector<ark::es2panda::lsp::TodoComment> (*getTodoComments)( 542 char const *fileName, std::vector<ark::es2panda::lsp::TodoCommentDescriptor> &descriptors, 543 ark::es2panda::lsp::CancellationToken *cancellationToken); 544 InlayHintList (*provideInlayHints)(es2panda_Context *context, const TextSpan *span); 545 SignatureHelpItems (*getSignatureHelpItems)(es2panda_Context *context, size_t position); 546 std::vector<CodeFixActionInfo> (*getCodeFixesAtPosition)(es2panda_Context *context, size_t start_position, 547 size_t end_position, std::vector<int> &errorCodes, 548 CodeFixOptions &codeFixOptions); 549 CombinedCodeActionsInfo (*getCombinedCodeFix)(const char *fileName, const std::string &fixId, 550 CodeFixOptions &codeFixOptions); 551 TextSpan *(*GetNameOrDottedNameSpan)(es2panda_Context *context, int startPos); 552 } LSPAPI; 553 CAPI_EXPORT LSPAPI const *GetImpl(); 554 // NOLINTEND 555 #ifdef __cplusplus 556 } 557 #endif 558 #endif