|
4 | 4 | * terms of the MIT License, which is available in the project root. |
5 | 5 | ******************************************************************************/ |
6 | 6 |
|
7 | | -import { describe, test } from 'vitest'; |
8 | | -import { EmptyFileSystem } from 'langium'; |
| 7 | +import { describe, expect, test } from 'vitest'; |
| 8 | +import { EmptyFileSystem, URI } from 'langium'; |
9 | 9 | import { createLangiumGrammarServices, createServicesForGrammar } from 'langium/grammar'; |
10 | 10 | import { expectGoToDefinition } from 'langium/test'; |
| 11 | +import { expandToString } from 'langium/generate'; |
| 12 | +import type { Range } from 'vscode-languageserver'; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * Represents a grammar file |
@@ -113,6 +115,57 @@ describe('Definition Provider', () => { |
113 | 115 | }); |
114 | 116 | }); |
115 | 117 | }); |
| 118 | + |
| 119 | + test('Should highlight full datatype rule node', async () => { |
| 120 | + const grammar = ` |
| 121 | + grammar Test |
| 122 | + entry Model: (elements+=Element)*; |
| 123 | + Element: Source | Target; |
| 124 | + Source: 'source' name=FQN; |
| 125 | + Target: 'target' ref=[Source]; |
| 126 | + FQN returns string: ID ('.' ID)*; |
| 127 | + terminal ID: /\\w+/; |
| 128 | + hidden terminal WS: /\\s+/; |
| 129 | + `; |
| 130 | + const services = await createServicesForGrammar({ grammar }); |
| 131 | + const text = expandToString` |
| 132 | + target a.b.c; |
| 133 | + source a.b.c; |
| 134 | + `; |
| 135 | + const workspace = services.shared.workspace; |
| 136 | + const document = workspace.LangiumDocumentFactory.fromString(text, URI.file('test.txt')); |
| 137 | + workspace.LangiumDocuments.addDocument(document); |
| 138 | + await workspace.DocumentBuilder.build([document]); |
| 139 | + const targetTextRange: Range = { |
| 140 | + start: document.textDocument.positionAt(text.indexOf('a.b.c')), |
| 141 | + end: document.textDocument.positionAt(text.indexOf('a.b.c') + 'a.b.c'.length) |
| 142 | + }; |
| 143 | + const sourceTextRange: Range = { |
| 144 | + start: document.textDocument.positionAt(text.lastIndexOf('a.b.c')), |
| 145 | + end: document.textDocument.positionAt(text.lastIndexOf('a.b.c') + 'a.b.c'.length) |
| 146 | + }; |
| 147 | + const provider = services.lsp.DefinitionProvider!; |
| 148 | + // Go to definition from target to source |
| 149 | + const defFromTarget = await provider.getDefinition(document, { |
| 150 | + textDocument: { uri: document.uri.toString() }, |
| 151 | + position: targetTextRange.start, |
| 152 | + }); |
| 153 | + expect(defFromTarget).toBeDefined(); |
| 154 | + expect(defFromTarget).toHaveLength(1); |
| 155 | + const targetSourceRange = defFromTarget![0].originSelectionRange!; |
| 156 | + expect(targetSourceRange).toBeDefined(); |
| 157 | + expect(targetSourceRange).toEqual(targetTextRange); |
| 158 | + // Go to definition from target to itself |
| 159 | + const defFromSource = await provider.getDefinition(document, { |
| 160 | + textDocument: { uri: document.uri.toString() }, |
| 161 | + position: sourceTextRange.start, |
| 162 | + }); |
| 163 | + expect(defFromSource).toBeDefined(); |
| 164 | + expect(defFromSource).toHaveLength(1); |
| 165 | + const sourceRange = defFromSource![0].originSelectionRange!; |
| 166 | + expect(sourceRange).toBeDefined(); |
| 167 | + expect(sourceRange).toEqual(sourceTextRange); |
| 168 | + }); |
116 | 169 | }); |
117 | 170 |
|
118 | 171 | describe('Definition Provider with Infix Operators', async () => { |
|
0 commit comments