|
| 1 | +import { createContextMapperDslServices } from '../../src/language/ContextMapperDslModule.js' |
| 2 | +import { clearDocuments, parseHelper } from 'langium/test' |
| 3 | +import { ContextMappingModel } from '../../src/language/generated/ast.js' |
| 4 | +import { EmptyFileSystem, LangiumDocument } from 'langium' |
| 5 | +import { SemanticTokenProvider } from 'langium/lsp' |
| 6 | +import { afterEach, beforeAll, describe, test } from 'vitest' |
| 7 | +import { |
| 8 | + createSemanticTokenParams, expectSemanticTokensToEqual, |
| 9 | + expectSemanticTokensToHaveLength, |
| 10 | + extractSemanticTokens |
| 11 | +} from './SemanticTokenTestHelper.js' |
| 12 | + |
| 13 | +let services: ReturnType<typeof createContextMapperDslServices> |
| 14 | +let parse: ReturnType<typeof parseHelper<ContextMappingModel>> |
| 15 | +let document: LangiumDocument<ContextMappingModel> | undefined |
| 16 | +let semanticTokenProvider: SemanticTokenProvider |
| 17 | + |
| 18 | +beforeAll(async () => { |
| 19 | + services = createContextMapperDslServices(EmptyFileSystem) |
| 20 | + parse = parseHelper<ContextMappingModel>(services.ContextMapperDsl) |
| 21 | + semanticTokenProvider = services.ContextMapperDsl.lsp.SemanticTokenProvider!! |
| 22 | +}) |
| 23 | + |
| 24 | +afterEach(async () => { |
| 25 | + document && await clearDocuments(services.shared, [document]) |
| 26 | +}) |
| 27 | + |
| 28 | +describe('Comment semantic token tests', () => { |
| 29 | + test('check semantic tokens for multiline comment at snippet start', async () => { |
| 30 | + document = await parse(` |
| 31 | + /* |
| 32 | + This is a multiline comment |
| 33 | + */ |
| 34 | + ContextMap {} |
| 35 | + `) |
| 36 | + const params = createSemanticTokenParams(document) |
| 37 | + |
| 38 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 39 | + |
| 40 | + const expectedNumberOfTokens = 2 |
| 41 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 42 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 43 | + |
| 44 | + expectSemanticTokensToEqual(tokens[0], 1, 6, 47, semanticTokenProvider.tokenTypes.comment, 0) |
| 45 | + }) |
| 46 | + |
| 47 | + test('check semantic tokens for multiline comment at snippet end', async () => { |
| 48 | + document = await parse(` |
| 49 | + ContextMap {} |
| 50 | + /* |
| 51 | + This is a multiline comment |
| 52 | + */ |
| 53 | + `) |
| 54 | + const params = createSemanticTokenParams(document) |
| 55 | + |
| 56 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 57 | + |
| 58 | + const expectedNumberOfTokens = 2 |
| 59 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 60 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 61 | + |
| 62 | + expectSemanticTokensToEqual(tokens[1], 1, 6, 47, semanticTokenProvider.tokenTypes.comment, 0) |
| 63 | + }) |
| 64 | + |
| 65 | + test('check semantic tokens for single-line comment at snippet start', async () => { |
| 66 | + document = await parse(` |
| 67 | + // This is a single-line comment |
| 68 | + ContextMap {} |
| 69 | + `) |
| 70 | + const params = createSemanticTokenParams(document) |
| 71 | + |
| 72 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 73 | + |
| 74 | + const expectedNumberOfTokens = 2 |
| 75 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 76 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 77 | + |
| 78 | + expectSemanticTokensToEqual(tokens[0], 1, 6, 32, semanticTokenProvider.tokenTypes.comment, 0) |
| 79 | + }) |
| 80 | + |
| 81 | + test('check semantic tokens for single-line comment at snippet end', async () => { |
| 82 | + document = await parse(` |
| 83 | + ContextMap {} |
| 84 | + // This is a single-line comment |
| 85 | + `) |
| 86 | + const params = createSemanticTokenParams(document) |
| 87 | + |
| 88 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 89 | + |
| 90 | + const expectedNumberOfTokens = 2 |
| 91 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 92 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 93 | + |
| 94 | + expectSemanticTokensToEqual(tokens[1], 1, 6, 32, semanticTokenProvider.tokenTypes.comment, 0) |
| 95 | + }) |
| 96 | + |
| 97 | + test('check semantic tokens for multiple multiline comments', async () => { |
| 98 | + document = await parse(` |
| 99 | + /* |
| 100 | + TestContext description |
| 101 | + */ |
| 102 | + BoundedContext TestContext |
| 103 | + /* |
| 104 | + AnotherContext description |
| 105 | + */ |
| 106 | + BoundedContext AnotherContext |
| 107 | + `) |
| 108 | + const params = createSemanticTokenParams(document) |
| 109 | + |
| 110 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 111 | + |
| 112 | + const expectedNumberOfTokens = 6 |
| 113 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 114 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 115 | + |
| 116 | + expectSemanticTokensToEqual(tokens[0], 1, 6, 43, semanticTokenProvider.tokenTypes.comment, 0) |
| 117 | + expectSemanticTokensToEqual(tokens[3], 1, 6, 46, semanticTokenProvider.tokenTypes.comment, 0) |
| 118 | + }) |
| 119 | + |
| 120 | + test('check semantic tokens for multiple single-line comments', async () => { |
| 121 | + document = await parse(` |
| 122 | + // TestContext description |
| 123 | + BoundedContext TestContext |
| 124 | + |
| 125 | + // AnotherContext description |
| 126 | + BoundedContext AnotherContext |
| 127 | + `) |
| 128 | + const params = createSemanticTokenParams(document) |
| 129 | + |
| 130 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 131 | + |
| 132 | + const expectedNumberOfTokens = 6 |
| 133 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 134 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 135 | + |
| 136 | + expectSemanticTokensToEqual(tokens[0], 1, 6, 26, semanticTokenProvider.tokenTypes.comment, 0) |
| 137 | + expectSemanticTokensToEqual(tokens[3], 2, 6, 29, semanticTokenProvider.tokenTypes.comment, 0) |
| 138 | + }) |
| 139 | + |
| 140 | + test('check semantic tokens for multiline comment in nested structure', async () => { |
| 141 | + document = await parse(` |
| 142 | + BoundedContext TestContext { |
| 143 | + /* |
| 144 | + This is a multiline comment |
| 145 | + */ |
| 146 | + Module TestModule |
| 147 | + } |
| 148 | + `) |
| 149 | + const params = createSemanticTokenParams(document) |
| 150 | + |
| 151 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 152 | + |
| 153 | + const expectedNumberOfTokens = 5 |
| 154 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 155 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 156 | + |
| 157 | + expectSemanticTokensToEqual(tokens[2], 1, 8, 51, semanticTokenProvider.tokenTypes.comment, 0) |
| 158 | + }) |
| 159 | + |
| 160 | + test('check semantic tokens for single-line comment in nested structure', async () => { |
| 161 | + document = await parse(` |
| 162 | + BoundedContext TestContext { |
| 163 | + // This is a multiline comment |
| 164 | + Module TestModule |
| 165 | + } |
| 166 | + `) |
| 167 | + const params = createSemanticTokenParams(document) |
| 168 | + |
| 169 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 170 | + |
| 171 | + const expectedNumberOfTokens = 5 |
| 172 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 173 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 174 | + |
| 175 | + expectSemanticTokensToEqual(tokens[2], 1, 8, 30, semanticTokenProvider.tokenTypes.comment, 0) |
| 176 | + }) |
| 177 | + |
| 178 | + test('check semantic tokens for single-line comment after a field', async () => { |
| 179 | + document = await parse(` |
| 180 | + BoundedContext TestContext { |
| 181 | + type TEAM // This is a single-line comment |
| 182 | + } |
| 183 | + `) |
| 184 | + const params = createSemanticTokenParams(document) |
| 185 | + |
| 186 | + const result = await semanticTokenProvider.semanticHighlight(document, params) |
| 187 | + |
| 188 | + const expectedNumberOfTokens = 5 |
| 189 | + expectSemanticTokensToHaveLength(result, expectedNumberOfTokens) |
| 190 | + const tokens = extractSemanticTokens(result, expectedNumberOfTokens) |
| 191 | + |
| 192 | + expectSemanticTokensToEqual(tokens[4], 0, 5, 32, semanticTokenProvider.tokenTypes.comment, 0) |
| 193 | + }) |
| 194 | +}) |
0 commit comments