|
| 1 | +package haxeLanguageServer.features.haxe; |
| 2 | + |
| 3 | +import haxeLanguageServer.features.haxe.refactoring.EditDoc; |
| 4 | +import haxeLanguageServer.features.haxe.refactoring.EditList; |
| 5 | +import haxeLanguageServer.features.haxe.refactoring.RefactorCache; |
| 6 | +import jsonrpc.CancellationToken; |
| 7 | +import jsonrpc.ResponseError; |
| 8 | +import jsonrpc.Types.NoData; |
| 9 | +import languageServerProtocol.Types.InlineValue; |
| 10 | +import languageServerProtocol.protocol.InlineValue; |
| 11 | +import refactor.discover.Identifier; |
| 12 | + |
| 13 | +class InlineValueFeature { |
| 14 | + final context:Context; |
| 15 | + final refactorCache:RefactorCache; |
| 16 | + |
| 17 | + public final converter:Haxe3DisplayOffsetConverter; |
| 18 | + |
| 19 | + public function new(context:Context, refactorCache:RefactorCache) { |
| 20 | + this.context = context; |
| 21 | + this.refactorCache = refactorCache; |
| 22 | + |
| 23 | + converter = new Haxe3DisplayOffsetConverter(); |
| 24 | + |
| 25 | + context.languageServerProtocol.onRequest(InlineValueRequest.type, onInlineValue); |
| 26 | + } |
| 27 | + |
| 28 | + function onInlineValue(params:InlineValueParams, token:CancellationToken, resolve:Array<InlineValue>->Void, reject:ResponseError<NoData>->Void) { |
| 29 | + if (context.config.user.disableRefactorCache || context.config.user.disableInlineValue) { |
| 30 | + resolve([]); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + var file = refactorCache.fileList.getFile(params.textDocument.uri.toFsPath().toString()); |
| 35 | + if (file == null) { |
| 36 | + reject.handler()("file not found"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + var editDoc = new EditDoc(params.textDocument.uri.toFsPath(), new EditList(), context, converter); |
| 41 | + |
| 42 | + var localScopedNames:Array<String> = []; |
| 43 | + |
| 44 | + function matchLocalScoped(identifier:Identifier):Bool { |
| 45 | + return switch (identifier.type) { |
| 46 | + case ScopedLocal(scopeStart, scopeEnd, scopeType): |
| 47 | + localScopedNames.push(identifier.name); |
| 48 | + true; |
| 49 | + case Access: true; |
| 50 | + default: false; |
| 51 | + } |
| 52 | + } |
| 53 | + final identifiers:Array<Identifier> = file.findAllIdentifiers(matchLocalScoped); |
| 54 | + final inlineValueVars:Array<InlineValue> = []; |
| 55 | + for (identifier in identifiers) { |
| 56 | + var identifierRange = editDoc.posToRange(identifier.pos); |
| 57 | + if (!params.range.contains(identifierRange)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + var needsExpression:Bool = identifier.name.contains("."); |
| 61 | + if ((identifier.type == Access) && !localScopedNames.contains(identifier.name)) { |
| 62 | + needsExpression = true; |
| 63 | + } |
| 64 | + |
| 65 | + if (needsExpression) { |
| 66 | + inlineValueVars.push({ |
| 67 | + range: identifierRange, |
| 68 | + expression: identifier.name |
| 69 | + }); |
| 70 | + } else { |
| 71 | + inlineValueVars.push({ |
| 72 | + range: identifierRange, |
| 73 | + variableName: identifier.name, |
| 74 | + caseSensitiveLookup: true |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + resolve(inlineValueVars); |
| 80 | + } |
| 81 | +} |
0 commit comments