Skip to content

Commit

Permalink
feat: add get references and definitions ability for entity
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoge committed Nov 14, 2024
1 parent 3275599 commit ee61c06
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 37 deletions.
118 changes: 111 additions & 7 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import type { ParseError } from 'dt-sql-parser';
import {
EntityContext,
} from 'dt-sql-parser/dist/parser/common/entityCollector';
import { WordPosition } from 'dt-sql-parser/dist/parser/common/textAndWord';
import * as monaco from 'monaco-editor';

import { BaseSQLWorker } from './baseSQLWorker';
import { debounce } from './common/utils';
import {
CancellationToken,
editor,
Uri,
IDisposable,
MarkerSeverity,
Range,
languages,
MarkerSeverity,
Position,
CancellationToken
Range,
Uri,
} from './fillers/monaco-editor-core';
import { debounce } from './common/utils';
import { BaseSQLWorker } from './baseSQLWorker';
import type { ParseError } from 'dt-sql-parser';
import type { LanguageServiceDefaults } from './monaco.contribution';

export interface WorkerAccessor<T extends BaseSQLWorker> {
Expand Down Expand Up @@ -197,3 +203,101 @@ export class CompletionAdapter<T extends BaseSQLWorker>
});
}
}

export class DefinitionAdapter<T extends BaseSQLWorker> implements languages.DefinitionProvider {
constructor(
private readonly _worker: WorkerAccessor<T>,
private readonly _defaults: LanguageServiceDefaults) {}
provideDefinition(
model: editor.IReadOnlyModel,
position: Position,
_token: CancellationToken
): languages.ProviderResult<languages.Definition | languages.LocationLink[]> {
const resource = model.uri;
const lineContent = model.getLineContent(position.lineNumber);
if (lineContent.startsWith('--')) return null;
return this._worker(resource)
.then((worker) => {
let code = model?.getValue() || '';
if (typeof this._defaults.preprocessCode === 'function') {
code = this._defaults.preprocessCode(code);
}
return worker.getAllEntities(code);
})
.then((entities) => {
const word = model.getWordAtPosition(position);
let pos: WordPosition = {
line: -1,
startIndex: -1,
endIndex: -1,
startColumn: -1,
endColumn: -1
};
entities?.forEach((entity: EntityContext) => {
if (
entity.entityContextType.includes('Create') &&
word?.word &&
entity.text === word?.word
) {
pos = entity.position;
}
});
if (pos && pos.line !== -1) {
return {
uri: model.uri,
range: new monaco.Range(
pos?.line,
pos?.startColumn,
pos?.line,
pos?.endColumn
)
};
}
});
}
}

export class ReferenceAdapter<T extends BaseSQLWorker> implements languages.ReferenceProvider {
constructor(
private readonly _worker: WorkerAccessor<T>,
private readonly _defaults: LanguageServiceDefaults
) {}
provideReferences(
model: editor.IReadOnlyModel,
position: Position,
_context: languages.ReferenceContext,
_token: CancellationToken
): languages.ProviderResult<languages.Location[]> {
const resource = model.uri;
const lineContent = model.getLineContent(position.lineNumber);
if (!lineContent.startsWith('CREATE')) return;
return this._worker(resource)
.then((worker) => {
let code = model?.getValue() || '';
if (typeof this._defaults.preprocessCode === 'function') {
code = this._defaults.preprocessCode(code);
}
return worker.getAllEntities(model?.getValue());
})
.then((entities) => {
const word = model.getWordAtPosition(position);
const arr: languages.Location[] = [];
entities?.forEach((entity) => {
if (word?.word && entity.text === word?.word) {
let pos: WordPosition | null = null;
pos = entity.position;
arr.push({
uri: model.uri,
range: new monaco.Range(
pos?.line,
pos?.startColumn,
pos?.line,
pos?.endColumn
)
});
}
});
return arr;
});
}
}
6 changes: 4 additions & 2 deletions src/languages/flink/flink.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.FLINK,
Expand All @@ -16,5 +16,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.FLINK, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
6 changes: 4 additions & 2 deletions src/languages/hive/hive.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.HIVE,
Expand All @@ -16,5 +16,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.HIVE, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
6 changes: 4 additions & 2 deletions src/languages/impala/impala.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.IMPALA,
Expand All @@ -16,5 +16,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.IMPALA, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
6 changes: 4 additions & 2 deletions src/languages/mysql/mysql.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.MYSQL,
Expand All @@ -16,5 +16,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.MYSQL, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
6 changes: 4 additions & 2 deletions src/languages/pgsql/pgsql.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.PG,
Expand All @@ -16,5 +16,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.PG, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
6 changes: 4 additions & 2 deletions src/languages/spark/spark.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.SPARK,
Expand All @@ -16,5 +16,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.SPARK, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
6 changes: 4 additions & 2 deletions src/languages/trino/trino.contribution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerLanguage } from '../../_.contribution';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';
import { LanguageIdEnum } from '../../common/constants';
import { setupLanguageFeatures } from '../../setupLanguageFeatures';

registerLanguage({
id: LanguageIdEnum.TRINO,
Expand All @@ -11,5 +11,7 @@ registerLanguage({

setupLanguageFeatures(LanguageIdEnum.TRINO, {
completionItems: true,
diagnostics: true
diagnostics: true,
references: true,
definitions: true,
});
23 changes: 18 additions & 5 deletions src/monaco.contribution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { languages, Emitter, IEvent, editor, Position, IRange } from './fillers/monaco-editor-core';
import { EntityContext, Suggestions } from 'dt-sql-parser';
import {
EntityContext,
Suggestions,
} from 'dt-sql-parser';

import {
editor,
Emitter,
IEvent,
IRange,
languages,
Position,
} from './fillers/monaco-editor-core';

/**
* A completion item.
Expand Down Expand Up @@ -65,7 +76,7 @@ export interface ModeConfiguration {
/**
* Defines whether the built-in definitions provider is enabled.
*/
// readonly definitions?: boolean;
readonly definitions?: boolean;

/**
* Defines whether the built-in rename provider is enabled.
Expand All @@ -75,7 +86,7 @@ export interface ModeConfiguration {
/**
* Defines whether the built-in references provider is enabled.
*/
// readonly references?: boolean;
readonly references?: boolean;
}

/**
Expand Down Expand Up @@ -171,5 +182,7 @@ export const modeConfigurationDefault: Required<ModeConfiguration> = {
completionService: defaultCompletionService,
triggerCharacters: ['.', ' ']
},
diagnostics: true
diagnostics: true,
definitions: true,
references: true
};
33 changes: 27 additions & 6 deletions src/setupLanguageFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { LanguageIdEnum } from './common/constants';
import {
PreprocessCode,
IDisposable,
languages,
} from './fillers/monaco-editor-core';
import {
CompletionOptions,
LanguageServiceDefaults,
LanguageServiceDefaultsImpl,
modeConfigurationDefault,
ModeConfiguration,
CompletionOptions
modeConfigurationDefault,
PreprocessCode,
} from './monaco.contribution';
import { languages, IDisposable } from './fillers/monaco-editor-core';
import { LanguageIdEnum } from './common/constants';

export interface FeatureConfiguration {
/**
Expand All @@ -20,6 +23,14 @@ export interface FeatureConfiguration {
* Defaults to true.
*/
diagnostics?: boolean;
/**
* Defines whether the built-in definitions provider is enabled.
*/
definitions?: boolean;
/**
* Defines whether the built-in references provider is enabled.
*/
references?: boolean;
/**
* Define a function to preprocess code.
* By default, do not something.
Expand Down Expand Up @@ -110,13 +121,23 @@ function processConfiguration(
? configuration.completionItems!.triggerCharacters
: (defaults?.modeConfiguration.completionItems.triggerCharacters ??
modeConfigurationDefault.completionItems.triggerCharacters);
const references =
typeof configuration.references === 'boolean'
? configuration.references
: (defaults?.modeConfiguration.references ?? modeConfigurationDefault.references);
const definitions =
typeof configuration.definitions === 'boolean'
? configuration.definitions
: (defaults?.modeConfiguration.definitions ?? modeConfigurationDefault.definitions);

return {
diagnostics,
completionItems: {
enable: completionEnable,
completionService,
triggerCharacters
}
},
references,
definitions,
};
}
Loading

0 comments on commit ee61c06

Please sign in to comment.