|
| 1 | +import type { ParseError } from 'dt-sql-parser'; |
1 | 2 | import {
|
| 3 | + EntityContext, |
| 4 | +} from 'dt-sql-parser/dist/parser/common/entityCollector'; |
| 5 | +import { WordPosition } from 'dt-sql-parser/dist/parser/common/textAndWord'; |
| 6 | +import * as monaco from 'monaco-editor'; |
| 7 | + |
| 8 | +import { BaseSQLWorker } from './baseSQLWorker'; |
| 9 | +import { debounce } from './common/utils'; |
| 10 | +import { |
| 11 | + CancellationToken, |
2 | 12 | editor,
|
3 |
| - Uri, |
4 | 13 | IDisposable,
|
5 |
| - MarkerSeverity, |
6 |
| - Range, |
7 | 14 | languages,
|
| 15 | + MarkerSeverity, |
8 | 16 | Position,
|
9 |
| - CancellationToken |
| 17 | + Range, |
| 18 | + Uri, |
10 | 19 | } from './fillers/monaco-editor-core';
|
11 |
| -import { debounce } from './common/utils'; |
12 |
| -import { BaseSQLWorker } from './baseSQLWorker'; |
13 |
| -import type { ParseError } from 'dt-sql-parser'; |
14 | 20 | import type { LanguageServiceDefaults } from './monaco.contribution';
|
15 | 21 |
|
16 | 22 | export interface WorkerAccessor<T extends BaseSQLWorker> {
|
@@ -197,3 +203,101 @@ export class CompletionAdapter<T extends BaseSQLWorker>
|
197 | 203 | });
|
198 | 204 | }
|
199 | 205 | }
|
| 206 | + |
| 207 | +export class DefinitionAdapter<T extends BaseSQLWorker> implements languages.DefinitionProvider { |
| 208 | + constructor( |
| 209 | + private readonly _worker: WorkerAccessor<T>, |
| 210 | + private readonly _defaults: LanguageServiceDefaults) {} |
| 211 | + provideDefinition( |
| 212 | + model: editor.IReadOnlyModel, |
| 213 | + position: Position, |
| 214 | + _token: CancellationToken |
| 215 | + ): languages.ProviderResult<languages.Definition | languages.LocationLink[]> { |
| 216 | + const resource = model.uri; |
| 217 | + const lineContent = model.getLineContent(position.lineNumber); |
| 218 | + if (lineContent.startsWith('--')) return null; |
| 219 | + return this._worker(resource) |
| 220 | + .then((worker) => { |
| 221 | + let code = model?.getValue() || ''; |
| 222 | + if (typeof this._defaults.preprocessCode === 'function') { |
| 223 | + code = this._defaults.preprocessCode(code); |
| 224 | + } |
| 225 | + return worker.getAllEntities(code); |
| 226 | + }) |
| 227 | + .then((entities) => { |
| 228 | + const word = model.getWordAtPosition(position); |
| 229 | + let pos: WordPosition = { |
| 230 | + line: -1, |
| 231 | + startIndex: -1, |
| 232 | + endIndex: -1, |
| 233 | + startColumn: -1, |
| 234 | + endColumn: -1 |
| 235 | + }; |
| 236 | + entities?.forEach((entity: EntityContext) => { |
| 237 | + if ( |
| 238 | + entity.entityContextType.includes('Create') && |
| 239 | + word?.word && |
| 240 | + entity.text === word?.word |
| 241 | + ) { |
| 242 | + pos = entity.position; |
| 243 | + } |
| 244 | + }); |
| 245 | + if (pos && pos.line !== -1) { |
| 246 | + return { |
| 247 | + uri: model.uri, |
| 248 | + range: new monaco.Range( |
| 249 | + pos?.line, |
| 250 | + pos?.startColumn, |
| 251 | + pos?.line, |
| 252 | + pos?.endColumn |
| 253 | + ) |
| 254 | + }; |
| 255 | + } |
| 256 | + }); |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +export class ReferenceAdapter<T extends BaseSQLWorker> implements languages.ReferenceProvider { |
| 261 | + constructor( |
| 262 | + private readonly _worker: WorkerAccessor<T>, |
| 263 | + private readonly _defaults: LanguageServiceDefaults |
| 264 | + ) {} |
| 265 | + provideReferences( |
| 266 | + model: editor.IReadOnlyModel, |
| 267 | + position: Position, |
| 268 | + _context: languages.ReferenceContext, |
| 269 | + _token: CancellationToken |
| 270 | + ): languages.ProviderResult<languages.Location[]> { |
| 271 | + const resource = model.uri; |
| 272 | + const lineContent = model.getLineContent(position.lineNumber); |
| 273 | + if (!lineContent.startsWith('CREATE')) return; |
| 274 | + return this._worker(resource) |
| 275 | + .then((worker) => { |
| 276 | + let code = model?.getValue() || ''; |
| 277 | + if (typeof this._defaults.preprocessCode === 'function') { |
| 278 | + code = this._defaults.preprocessCode(code); |
| 279 | + } |
| 280 | + return worker.getAllEntities(model?.getValue()); |
| 281 | + }) |
| 282 | + .then((entities) => { |
| 283 | + const word = model.getWordAtPosition(position); |
| 284 | + const arr: languages.Location[] = []; |
| 285 | + entities?.forEach((entity) => { |
| 286 | + if (word?.word && entity.text === word?.word) { |
| 287 | + let pos: WordPosition | null = null; |
| 288 | + pos = entity.position; |
| 289 | + arr.push({ |
| 290 | + uri: model.uri, |
| 291 | + range: new monaco.Range( |
| 292 | + pos?.line, |
| 293 | + pos?.startColumn, |
| 294 | + pos?.line, |
| 295 | + pos?.endColumn |
| 296 | + ) |
| 297 | + }); |
| 298 | + } |
| 299 | + }); |
| 300 | + return arr; |
| 301 | + }); |
| 302 | + } |
| 303 | +} |
0 commit comments