|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Collections.Immutable; |
7 | 7 | using System.Linq; |
8 | | -using Microsoft.Quantum.QsCompiler.CompilationBuilder.DataStructures; |
9 | 8 | using Microsoft.Quantum.QsCompiler.DataTypes; |
10 | 9 | using Microsoft.Quantum.QsCompiler.SyntaxProcessing; |
11 | 10 | using Microsoft.Quantum.QsCompiler.SyntaxTokens; |
@@ -48,26 +47,29 @@ internal static class EditorCommands |
48 | 47 | /// </summary> |
49 | 48 | public static Location? DefinitionLocation(this FileContentManager file, CompilationUnit compilation, Position? position) |
50 | 49 | { |
51 | | - var symbolInfo = file?.TryGetQsSymbolInfo(position, true, out CodeFragment _); // includes the end position |
52 | | - if (file is null || symbolInfo is null || compilation is null || position is null) |
| 50 | + var fragment = file.TryGetFragmentAt(position, out _, true); |
| 51 | + if (position is null || fragment is null) |
| 52 | + { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + var occurrence = SymbolInfo.SymbolOccurrence(fragment, position, true); |
| 57 | + if (occurrence is null) |
53 | 58 | { |
54 | 59 | return null; |
55 | 60 | } |
56 | 61 |
|
57 | 62 | var locals = compilation.TryGetLocalDeclarations(file, position, out var cName, includeDeclaredAtPosition: true); |
58 | | - if (cName == null) |
| 63 | + if (cName is null) |
59 | 64 | { |
60 | 65 | return null; |
61 | 66 | } |
62 | 67 |
|
63 | | - var found = |
64 | | - symbolInfo.UsedVariables.Any() |
65 | | - ? compilation.GlobalSymbols.VariableDeclaration(locals, cName.Namespace, file.FileName, symbolInfo.UsedVariables.Single()) |
66 | | - : symbolInfo.UsedTypes.Any() |
67 | | - ? compilation.GlobalSymbols.TypeDeclaration(cName.Namespace, file.FileName, symbolInfo.UsedTypes.Single()) |
68 | | - : symbolInfo.DeclaredSymbols.Any() |
69 | | - ? compilation.GlobalSymbols.SymbolDeclaration(locals, cName.Namespace, file.FileName, symbolInfo.DeclaredSymbols.Single()) |
70 | | - : QsNullable<Tuple<string, Position, Range>>.Null; |
| 68 | + var found = occurrence.Match( |
| 69 | + declaration: s => compilation.GlobalSymbols.SymbolDeclaration(locals, cName.Namespace, file.FileName, s), |
| 70 | + usedType: t => compilation.GlobalSymbols.TypeDeclaration(cName.Namespace, file.FileName, t), |
| 71 | + usedVariable: s => compilation.GlobalSymbols.VariableDeclaration(locals, cName.Namespace, file.FileName, s), |
| 72 | + usedLiteral: e => QsNullable<Tuple<string, Position, Range>>.Null); |
71 | 73 |
|
72 | 74 | return found.IsValue |
73 | 75 | ? SymbolInfo.AsLocation(found.Item.Item1, found.Item.Item2, found.Item.Item3) |
@@ -221,44 +223,38 @@ DocumentHighlight AsHighlight(Lsp.Range range) => |
221 | 223 | public static Hover? HoverInformation( |
222 | 224 | this FileContentManager file, |
223 | 225 | CompilationUnit compilation, |
224 | | - Position? position, |
| 226 | + Position position, |
225 | 227 | MarkupKind format = MarkupKind.PlainText) |
226 | 228 | { |
227 | | - Hover? GetHover(string? info) => info == null ? null : new Hover |
228 | | - { |
229 | | - Contents = new MarkupContent { Kind = format, Value = info }, |
230 | | - Range = new Lsp.Range { Start = position.ToLsp(), End = position.ToLsp() }, |
231 | | - }; |
232 | | - |
| 229 | + // TODO: Add hover for functor generators and functor applications. |
| 230 | + // TODO: Add hover for new array expressions. |
| 231 | + // TODO: Add nested types (requires SymbolInfo.SymbolOccurrence to return the closest match). |
233 | 232 | var markdown = format == MarkupKind.Markdown; |
234 | | - var symbolInfo = file?.TryGetQsSymbolInfo(position, false, out var _); |
235 | | - if (file is null || symbolInfo == null || compilation == null || position is null) |
236 | | - { |
237 | | - return null; |
238 | | - } |
239 | | - |
240 | | - if (symbolInfo.UsedLiterals.Any()) |
241 | | - { |
242 | | - return GetHover(symbolInfo.UsedLiterals.Single().LiteralInfo(markdown)); |
243 | | - } |
| 233 | + var fragment = file.TryGetFragmentAt(position, out _); |
| 234 | + var occurrence = fragment is null ? null : SymbolInfo.SymbolOccurrence(fragment, position, false); |
| 235 | + var info = occurrence?.Match( |
| 236 | + declaration: s => WithContext((locals, nsName) => |
| 237 | + compilation.GlobalSymbols.DeclarationInfo(locals, nsName, file.FileName, s, markdown)), |
| 238 | + usedType: t => WithContext((locals, nsName) => |
| 239 | + compilation.GlobalSymbols.TypeInfo(nsName, file.FileName, t, markdown)), |
| 240 | + usedVariable: s => WithContext((locals, nsName) => |
| 241 | + compilation.GlobalSymbols.VariableInfo(locals, nsName, file.FileName, s, markdown)), |
| 242 | + usedLiteral: e => e.LiteralInfo(markdown)); |
| 243 | + |
| 244 | + return info is null |
| 245 | + ? null |
| 246 | + : new Hover |
| 247 | + { |
| 248 | + Contents = new MarkupContent { Kind = format, Value = info }, |
| 249 | + Range = new Lsp.Range { Start = position.ToLsp(), End = position.ToLsp() }, |
| 250 | + }; |
244 | 251 |
|
245 | | - var locals = compilation.TryGetLocalDeclarations(file, position, out var cName, includeDeclaredAtPosition: true); |
246 | | - var nsName = cName?.Namespace ?? file.TryGetNamespaceAt(position); |
247 | | - if (nsName == null) |
| 252 | + string? WithContext(Func<LocalDeclarations, string, string?> f) |
248 | 253 | { |
249 | | - return null; |
| 254 | + var locals = compilation.TryGetLocalDeclarations(file, position, out var cName, includeDeclaredAtPosition: true); |
| 255 | + var nsName = cName?.Namespace ?? file.TryGetNamespaceAt(position); |
| 256 | + return nsName is null ? null : f(locals, nsName); |
250 | 257 | } |
251 | | - |
252 | | - // TODO: add hover for functor generators and functor applications |
253 | | - // TODO: add hover for new array expr ? |
254 | | - // TODO: add nested types - requires dropping the .Single and actually resolving to the closest match! |
255 | | - return GetHover(symbolInfo.UsedVariables.Any() |
256 | | - ? compilation.GlobalSymbols.VariableInfo(locals, nsName, file.FileName, symbolInfo.UsedVariables.Single(), markdown) |
257 | | - : symbolInfo.UsedTypes.Any() |
258 | | - ? compilation.GlobalSymbols.TypeInfo(nsName, file.FileName, symbolInfo.UsedTypes.Single(), markdown) |
259 | | - : symbolInfo.DeclaredSymbols.Any() |
260 | | - ? compilation.GlobalSymbols.DeclarationInfo(locals, nsName, file.FileName, symbolInfo.DeclaredSymbols.Single(), markdown) |
261 | | - : null); |
262 | 258 | } |
263 | 259 |
|
264 | 260 | /// <summary> |
|
0 commit comments