Debian Codegraph @

LspCpp/src/lsp/textDocument.cpp

raw
1 #include "LibLsp/lsp/textDocument/completion.h"
2 #include "LibLsp/lsp/textDocument/document_symbol.h"
3 #include "LibLsp/lsp/lsMarkedString.h"
4 #include "LibLsp/lsp/textDocument/hover.h"
5 #include "LibLsp/lsp/textDocument/prepareRename.h"
6 #include <LibLsp/lsp/textDocument/typeHierarchy.h>
7
8 #include "LibLsp/lsp/textDocument/semanticHighlighting.h"
9 #include "LibLsp/lsp/textDocument/SemanticTokens.h"
10 #include "LibLsp/JsonRpc/json.h"
11
12 constexpr unsigned SemanticTokenEncodingSize = 5;
13
14 std::string to_string(SemanticTokenType _type)
15 {
16 switch (_type)
17 {
18
19 case ls_namespace:
20 return "namespace";
21 /**
22 * Represents a generic type. Acts as a fallback for types which
23 * can"t be mapped to a specific type like class or enum.
24 */
25 case ls_type:
26 return "type";
27 case ls_class:
28 return "class";
29 case ls_enum:
30 return "enum";
31 case ls_interface:
32 return "interface";
33 case ls_struct:
34 return "struct";
35 case ls_typeParameter:
36 return "typeParameter";
37 case ls_parameter:
38 return "parameter";
39 case ls_variable:
40 return "variable";
41 case ls_property:
42 return "property";
43 case ls_enumMember:
44 return "enumMember";
45 case ls_event:
46 return "event";
47 case ls_function:
48 return "function";
49 case ls_method:
50 return "method";
51 case ls_macro:
52 return "macro";
53 case ls_keyword:
54 return "keyword";
55 case ls_modifier:
56 return "modifier";
57 case ls_comment:
58 return "comment";
59 case ls_string:
60 return "string";
61 case ls_number:
62 return "number";
63 case ls_regexp:
64 return "regexp";
65 case ls_operator:
66 return "operator";
67 default:
68 return "unknown";
69 }
70 }
71
72 unsigned toSemanticTokenType(std::vector<SemanticTokenType>& modifiers)
73 {
74 unsigned encode_type = 0;
75 for (auto bit : modifiers)
76 {
77 encode_type = encode_type | (0b00000001 << bit);
78 }
79 return encode_type;
80 }
81
82 std::string to_string(TokenType_JDT _type)
83 {
84 switch (_type)
85 {
86 case PACKAGE_JDT:
87 return "namespace";
88 case CLASS_JDT:
89 return "class";
90 case INTERFACE_JDT:
91 return "interface";
92 case ENUM_JDT:
93 return "enum";
94 case ENUM_MEMBER_JDT:
95 return "enumMember";
96 case TYPE_JDT:
97 return "type";
98 case TYPE_PARAMETER_JDT:
99 return "typeParameter";
100 case ANNOTATION_JDT:
101 return "annotation";
102 case ANNOTATION_MEMBER_JDT:
103 return "annotationMember";
104 case METHOD_JDT:
105 return "function";
106 case PROPERTY_JDT:
107 return "property";
108 case VARIABLE_JDT:
109 return "variable";
110 case PARAMETER_JDT:
111 return "parameter";
112 }
113 return "unknown";
114 }
115
116 std::string to_string(SemanticTokenModifier modifier)
117 {
118 switch (modifier)
119 {
120 case ls_declaration:
121 return "declaration";
122 case ls_definition:
123 return "definition";
124 case ls_readonly:
125 return "readonly";
126 case ls_static:
127 return "static";
128 case ls_deprecated:
129 return "deprecated";
130 case ls_abstract:
131 return "abstract";
132 case ls_async:
133 return "async";
134 case ls_modification:
135 return "modification";
136 case ls_documentation:
137 return "documentation";
138 case ls_defaultLibrary:
139 return "defaultLibrary";
140 default:
141 return "unknown";
142 }
143 }
144
145 unsigned toSemanticTokenModifiers(std::vector<SemanticTokenModifier>& modifiers)
146 {
147 unsigned encodedModifiers = 0;
148 for (auto bit : modifiers)
149 {
150 encodedModifiers = encodedModifiers | (0b00000001 << bit);
151 }
152 return encodedModifiers;
153 }
154
155 std::string toSemanticTokenType(HighlightingKind_clangD kind)
156 {
157 switch (kind)
158 {
159 case HighlightingKind_clangD::Variable:
160 case HighlightingKind_clangD::LocalVariable:
161 case HighlightingKind_clangD::StaticField:
162 return "variable";
163 case HighlightingKind_clangD::Parameter:
164 return "parameter";
165 case HighlightingKind_clangD::Function:
166 return "function";
167 case HighlightingKind_clangD::Method:
168 return "method";
169 case HighlightingKind_clangD::StaticMethod:
170 // FIXME: better method with static modifier?
171 return "function";
172 case HighlightingKind_clangD::Field:
173 return "property";
174 case HighlightingKind_clangD::Class:
175 return "class";
176 case HighlightingKind_clangD::Interface:
177 return "interface";
178 case HighlightingKind_clangD::Enum:
179 return "enum";
180 case HighlightingKind_clangD::EnumConstant:
181 return "enumMember";
182 case HighlightingKind_clangD::Typedef:
183 case HighlightingKind_clangD::Type:
184 return "type";
185 case HighlightingKind_clangD::Unknown:
186 return "unknown"; // nonstandard
187 case HighlightingKind_clangD::Namespace:
188 return "namespace";
189 case HighlightingKind_clangD::TemplateParameter:
190 return "typeParameter";
191 case HighlightingKind_clangD::Concept:
192 return "concept"; // nonstandard
193 case HighlightingKind_clangD::Primitive:
194 return "type";
195 case HighlightingKind_clangD::Macro:
196 return "macro";
197 case HighlightingKind_clangD::InactiveCode:
198 return "comment";
199 }
200 return ("unhandled HighlightingKind_clangD");
201 }
202
203 std::string toSemanticTokenModifier(HighlightingModifier_clangD modifier)
204 {
205 switch (modifier)
206 {
207 case HighlightingModifier_clangD::Declaration:
208 return "declaration";
209 case HighlightingModifier_clangD::Deprecated:
210 return "deprecated";
211 case HighlightingModifier_clangD::Readonly:
212 return "readonly";
213 case HighlightingModifier_clangD::Static:
214 return "static";
215 case HighlightingModifier_clangD::Deduced:
216 return "deduced"; // nonstandard
217 case HighlightingModifier_clangD::Abstract:
218 return "abstract";
219 case HighlightingModifier_clangD::DependentName:
220 return "dependentName"; // nonstandard
221 case HighlightingModifier_clangD::DefaultLibrary:
222 return "defaultLibrary";
223 case HighlightingModifier_clangD::FunctionScope:
224 return "functionScope"; // nonstandard
225 case HighlightingModifier_clangD::ClassScope:
226 return "classScope"; // nonstandard
227 case HighlightingModifier_clangD::FileScope:
228 return "fileScope"; // nonstandard
229 case HighlightingModifier_clangD::GlobalScope:
230 return "globalScope"; // nonstandard
231 }
232 return ("unhandled HighlightingModifier_clangD");
233 }
234
235 bool operator==(SemanticToken const& l, SemanticToken const& r)
236 {
237 return std::tie(l.deltaLine, l.deltaStart, l.length, l.tokenType, l.tokenModifiers)
238 == std::tie(r.deltaLine, r.deltaStart, r.length, r.tokenType, r.tokenModifiers);
239 }
240
241 std::vector<int32_t> SemanticTokens::encodeTokens(std::vector<SemanticToken>& tokens)
242 {
243 std::vector<int32_t> result;
244 result.reserve(SemanticTokenEncodingSize * tokens.size());
245 for (auto const& tok : tokens)
246 {
247 result.push_back(tok.deltaLine);
248 result.push_back(tok.deltaStart);
249 result.push_back(tok.length);
250 result.push_back(tok.tokenType);
251 result.push_back(tok.tokenModifiers);
252 }
253 assert(result.size() == SemanticTokenEncodingSize * tokens.size());
254 return result;
255 }
256
257 void Reflect(Reader& visitor, TextDocumentComplete::Either& value)
258 {
259 if (visitor.IsArray())
260 {
261 Reflect(visitor, value.first);
262 }
263 else
264 {
265
266 Reflect(visitor, value.second);
267 }
268 }
269 void Reflect(Reader& visitor, TextDocumentDocumentSymbol::Either& value)
270 {
271 if (visitor.HasMember("location"))
272 {
273 Reflect(visitor, value.first);
274 }
275 else
276 {
277 Reflect(visitor, value.second);
278 }
279 }
280
281 void Reflect(Reader& visitor, std::pair<optional<std::string>, optional<lsMarkedString>>& value)
282 {
283
284 if (!visitor.IsString())
285 {
286 Reflect(visitor, value.second);
287 }
288 else
289 {
290 Reflect(visitor, value.first);
291 }
292 }
293
294 void Reflect(Reader& visitor, std::pair<optional<std::string>, optional<MarkupContent>>& value)
295 {
296 if (!visitor.IsString())
297 {
298 Reflect(visitor, value.second);
299 }
300 else
301 {
302 Reflect(visitor, value.first);
303 }
304 }
305 void Reflect(Reader& visitor, TextDocumentHover::Either& value)
306 {
307 JsonReader& reader = dynamic_cast<JsonReader&>(visitor);
308 if (reader.IsArray())
309 {
310 Reflect(visitor, value.first);
311 }
312 else if (reader.m_->IsObject())
313 {
314 Reflect(visitor, value.second);
315 }
316 }
317
318 void Reflect(Reader& visitor, TextDocumentPrepareRenameResult& value)
319 {
320 if (visitor.HasMember("placeholder"))
321 {
322 Reflect(visitor, value.second);
323 }
324 else
325 {
326 Reflect(visitor, value.first);
327 }
328 }
329
330 namespace RefactorProposalUtility
331 {
332 char const* APPLY_REFACTORING_COMMAND_ID = "java.action.applyRefactoringCommand";
333 char const* EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND = "extractVariableAllOccurrence";
334 char const* EXTRACT_VARIABLE_COMMAND = "extractVariable";
335 char const* EXTRACT_CONSTANT_COMMAND = "extractConstant";
336 char const* EXTRACT_METHOD_COMMAND = "extractMethod";
337 char const* EXTRACT_FIELD_COMMAND = "extractField";
338 char const* CONVERT_VARIABLE_TO_FIELD_COMMAND = "convertVariableToField";
339 char const* MOVE_FILE_COMMAND = "moveFile";
340 char const* MOVE_INSTANCE_METHOD_COMMAND = "moveInstanceMethod";
341 char const* MOVE_STATIC_MEMBER_COMMAND = "moveStaticMember";
342 char const* MOVE_TYPE_COMMAND = "moveType";
343 }; // namespace RefactorProposalUtility
344 namespace QuickAssistProcessor
345 {
346
347 char const* SPLIT_JOIN_VARIABLE_DECLARATION_ID =
348 "org.eclipse.jdt.ls.correction.splitJoinVariableDeclaration.assist"; //$NON-NLS-1$
349 char const* CONVERT_FOR_LOOP_ID = "org.eclipse.jdt.ls.correction.convertForLoop.assist"; //$NON-NLS-1$
350 char const* ASSIGN_TO_LOCAL_ID = "org.eclipse.jdt.ls.correction.assignToLocal.assist"; //$NON-NLS-1$
351 char const* ASSIGN_TO_FIELD_ID = "org.eclipse.jdt.ls.correction.assignToField.assist"; //$NON-NLS-1$
352 char const* ASSIGN_PARAM_TO_FIELD_ID = "org.eclipse.jdt.ls.correction.assignParamToField.assist"; //$NON-NLS-1$
353 char const* ASSIGN_ALL_PARAMS_TO_NEW_FIELDS_ID =
354 "org.eclipse.jdt.ls.correction.assignAllParamsToNewFields.assist"; //$NON-NLS-1$
355 char const* ADD_BLOCK_ID = "org.eclipse.jdt.ls.correction.addBlock.assist"; //$NON-NLS-1$
356 char const* EXTRACT_LOCAL_ID = "org.eclipse.jdt.ls.correction.extractLocal.assist"; //$NON-NLS-1$
357 char const* EXTRACT_LOCAL_NOT_REPLACE_ID =
358 "org.eclipse.jdt.ls.correction.extractLocalNotReplaceOccurrences.assist"; //$NON-NLS-1$
359 char const* EXTRACT_CONSTANT_ID = "org.eclipse.jdt.ls.correction.extractConstant.assist"; //$NON-NLS-1$
360 char const* INLINE_LOCAL_ID = "org.eclipse.jdt.ls.correction.inlineLocal.assist"; //$NON-NLS-1$
361 char const* CONVERT_LOCAL_TO_FIELD_ID = "org.eclipse.jdt.ls.correction.convertLocalToField.assist"; //$NON-NLS-1$
362 char const* CONVERT_ANONYMOUS_TO_LOCAL_ID =
363 "org.eclipse.jdt.ls.correction.convertAnonymousToLocal.assist"; //$NON-NLS-1$
364 char const* CONVERT_TO_STRING_BUFFER_ID = "org.eclipse.jdt.ls.correction.convertToStringBuffer.assist"; //$NON-NLS-1$
365 char const* CONVERT_TO_MESSAGE_FORMAT_ID = "org.eclipse.jdt.ls.correction.convertToMessageFormat.assist"; //$NON-NLS-1$;
366 char const* EXTRACT_METHOD_INPLACE_ID = "org.eclipse.jdt.ls.correction.extractMethodInplace.assist"; //$NON-NLS-1$;
367
368 char const* CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND = "convertAnonymousClassToNestedCommand";
369 }; // namespace QuickAssistProcessor
370
371 void Reflect(Reader& reader, TypeHierarchyDirection& value)
372 {
373 if (!reader.IsString())
374 {
375 value = TypeHierarchyDirection::Both;
376 return;
377 }
378 std::string v = reader.GetString();
379 if (v == "Children")
380 {
381 value = TypeHierarchyDirection::Both;
382 }
383 else if (v == "Parents")
384 {
385 value = TypeHierarchyDirection::Parents;
386 }
387 else if (v == "Both")
388 {
389 value = TypeHierarchyDirection::Both;
390 }
391 }
392
393 void Reflect(Writer& writer, TypeHierarchyDirection& value)
394 {
395 switch (value)
396 {
397 case TypeHierarchyDirection::Children:
398 writer.String("Children");
399 break;
400 case TypeHierarchyDirection::Parents:
401 writer.String("Parents");
402 break;
403 case TypeHierarchyDirection::Both:
404 writer.String("Both");
405 break;
406 }
407 }