|
| 1 | +import type { ParseError } from 'dt-sql-parser'; |
| 2 | +import { EntityContext } from 'dt-sql-parser/dist/parser/common/entityCollector'; |
| 3 | +import { WordPosition } from 'dt-sql-parser/dist/parser/common/textAndWord'; |
| 4 | +import * as monaco from 'monaco-editor'; |
| 5 | + |
| 6 | +import { BaseSQLWorker } from './baseSQLWorker'; |
| 7 | +import { debounce } from './common/utils'; |
1 | 8 | import { |
| 9 | + CancellationToken, |
2 | 10 | editor, |
3 | | - Uri, |
4 | 11 | IDisposable, |
5 | | - MarkerSeverity, |
6 | | - Range, |
7 | 12 | languages, |
| 13 | + MarkerSeverity, |
8 | 14 | Position, |
9 | | - CancellationToken |
| 15 | + Range, |
| 16 | + Uri |
10 | 17 | } 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 | 18 | import type { LanguageServiceDefaults } from './monaco.contribution'; |
15 | 19 |
|
16 | 20 | export interface WorkerAccessor<T extends BaseSQLWorker> { |
@@ -197,3 +201,145 @@ export class CompletionAdapter<T extends BaseSQLWorker> |
197 | 201 | }); |
198 | 202 | } |
199 | 203 | } |
| 204 | +/** |
| 205 | + * The adapter is for the definition of the symbol at the given position and document. |
| 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 | + ) {} |
| 212 | + /** |
| 213 | + * Provide the definition of the symbol at the given position and document. |
| 214 | + **/ |
| 215 | + provideDefinition( |
| 216 | + model: editor.IReadOnlyModel, |
| 217 | + position: Position, |
| 218 | + _token: CancellationToken |
| 219 | + ): languages.ProviderResult<languages.Definition | languages.LocationLink[]> { |
| 220 | + const resource = model.uri; |
| 221 | + const lineContent = model.getLineContent(position.lineNumber); |
| 222 | + if (lineContent.startsWith('--')) return null; |
| 223 | + return this._worker(resource) |
| 224 | + .then((worker) => { |
| 225 | + let code = model?.getValue() || ''; |
| 226 | + if (typeof this._defaults.preprocessCode === 'function') { |
| 227 | + code = this._defaults.preprocessCode(code); |
| 228 | + } |
| 229 | + return worker.getAllEntities(code); |
| 230 | + }) |
| 231 | + .then((entities) => { |
| 232 | + const word = model.getWordAtPosition(position); |
| 233 | + let pos: WordPosition = { |
| 234 | + line: -1, |
| 235 | + startIndex: -1, |
| 236 | + endIndex: -1, |
| 237 | + startColumn: -1, |
| 238 | + endColumn: -1 |
| 239 | + }; |
| 240 | + const curEntity = entities?.find((entity: EntityContext) => { |
| 241 | + const entityPosition = entity.position; |
| 242 | + if ( |
| 243 | + entityPosition.startColumn === word?.startColumn && |
| 244 | + entityPosition.endColumn === word?.endColumn && |
| 245 | + entityPosition.line === position.lineNumber |
| 246 | + ) { |
| 247 | + return entity; |
| 248 | + } |
| 249 | + return null; |
| 250 | + }); |
| 251 | + if (curEntity) { |
| 252 | + for (let k in entities) { |
| 253 | + const entity = entities[Number(k)]; |
| 254 | + if ( |
| 255 | + entity.entityContextType.includes('Create') && |
| 256 | + word?.word && |
| 257 | + entity.text === word?.word && |
| 258 | + entity.entityContextType.includes(curEntity.entityContextType) |
| 259 | + ) { |
| 260 | + pos = entity.position; |
| 261 | + break; |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + if (pos && pos.line !== -1) { |
| 266 | + return { |
| 267 | + uri: model.uri, |
| 268 | + range: new monaco.Range( |
| 269 | + pos?.line, |
| 270 | + pos?.startColumn, |
| 271 | + pos?.line, |
| 272 | + pos?.endColumn |
| 273 | + ) |
| 274 | + }; |
| 275 | + } |
| 276 | + }); |
| 277 | + } |
| 278 | +} |
| 279 | +/** |
| 280 | + * The adapter is for the references of the symbol at the given position and document. |
| 281 | + **/ |
| 282 | +export class ReferenceAdapter<T extends BaseSQLWorker> implements languages.ReferenceProvider { |
| 283 | + constructor( |
| 284 | + private readonly _worker: WorkerAccessor<T>, |
| 285 | + private readonly _defaults: LanguageServiceDefaults |
| 286 | + ) {} |
| 287 | + /** |
| 288 | + * Provide a set of project-wide references for the given position and document. |
| 289 | + **/ |
| 290 | + provideReferences( |
| 291 | + model: editor.IReadOnlyModel, |
| 292 | + position: Position, |
| 293 | + _context: languages.ReferenceContext, |
| 294 | + _token: CancellationToken |
| 295 | + ): languages.ProviderResult<languages.Location[]> { |
| 296 | + const resource = model.uri; |
| 297 | + const lineContent = model.getLineContent(position.lineNumber); |
| 298 | + if (!lineContent.startsWith('CREATE')) return; |
| 299 | + return this._worker(resource) |
| 300 | + .then((worker) => { |
| 301 | + let code = model?.getValue() || ''; |
| 302 | + if (typeof this._defaults.preprocessCode === 'function') { |
| 303 | + code = this._defaults.preprocessCode(code); |
| 304 | + } |
| 305 | + return worker.getAllEntities(model?.getValue()); |
| 306 | + }) |
| 307 | + .then((entities) => { |
| 308 | + const word = model.getWordAtPosition(position); |
| 309 | + const arr: languages.Location[] = []; |
| 310 | + const curEntity = entities?.find((entity: EntityContext) => { |
| 311 | + const entityPosition = entity.position; |
| 312 | + if ( |
| 313 | + entityPosition.startColumn === word?.startColumn && |
| 314 | + entityPosition.endColumn === word?.endColumn && |
| 315 | + entityPosition.line === position.lineNumber |
| 316 | + ) { |
| 317 | + return entity; |
| 318 | + } |
| 319 | + return null; |
| 320 | + }); |
| 321 | + if (curEntity) { |
| 322 | + entities?.forEach((entity) => { |
| 323 | + if ( |
| 324 | + word?.word && |
| 325 | + entity.text === word?.word && |
| 326 | + curEntity.entityContextType.includes(entity.entityContextType) |
| 327 | + ) { |
| 328 | + let pos: WordPosition | null = null; |
| 329 | + pos = entity.position; |
| 330 | + arr.push({ |
| 331 | + uri: model.uri, |
| 332 | + range: new monaco.Range( |
| 333 | + pos?.line, |
| 334 | + pos?.startColumn, |
| 335 | + pos?.line, |
| 336 | + pos?.endColumn |
| 337 | + ) |
| 338 | + }); |
| 339 | + } |
| 340 | + }); |
| 341 | + } |
| 342 | + return arr; |
| 343 | + }); |
| 344 | + } |
| 345 | +} |
0 commit comments