Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reference and definition provider #145

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 143 additions & 7 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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 +201,135 @@ export class CompletionAdapter<T extends BaseSQLWorker>
});
}
}

export class DefinitionAdapter<T extends BaseSQLWorker> implements languages.DefinitionProvider {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看下两个 Provider 要不要加下注释

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是否还要考虑块级注释

/* DROP TABLE IF EXISTS aabb; */
-- DROP TABLE IF EXISTS aabb;
DROP TABLE IF EXISTS aabb;

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
};
const curEntity = entities?.find((entity: EntityContext) => {
const entityPosition = entity.position;
if (
entityPosition.startColumn === word?.startColumn &&
entityPosition.endColumn === word?.endColumn &&
entityPosition.line === position.lineNumber
) {
return entity;
}
return null;
});
if (curEntity) {
for (let k in entities) {
const entity = entities[Number(k)];
if (
entity.entityContextType.includes('Create') &&
word?.word &&
entity.text === word?.word &&
entity.entityContextType.includes(curEntity.entityContextType)
) {
pos = entity.position;
break;
}
}
}
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[] = [];
const curEntity = entities?.find((entity: EntityContext) => {
const entityPosition = entity.position;
if (
entityPosition.startColumn === word?.startColumn &&
entityPosition.endColumn === word?.endColumn &&
entityPosition.line === position.lineNumber
) {
return entity;
}
return null;
});
if (curEntity) {
entities?.forEach((entity) => {
if (
word?.word &&
entity.text === word?.word &&
curEntity.entityContextType.includes(entity.entityContextType)
) {
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
});
11 changes: 7 additions & 4 deletions src/monaco.contribution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { languages, Emitter, IEvent, editor, Position, IRange } from './fillers/monaco-editor-core';
import { EntityContext, Suggestions } from 'dt-sql-parser';

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

/**
* A completion item.
* ICompletionItem is pretty much the same as {@link languages.CompletionItem},
Expand Down Expand Up @@ -65,7 +66,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 +76,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 +172,7 @@ export const modeConfigurationDefault: Required<ModeConfiguration> = {
completionService: defaultCompletionService,
triggerCharacters: ['.', ' ']
},
diagnostics: true
diagnostics: true,
definitions: true,
references: true
};
30 changes: 24 additions & 6 deletions src/setupLanguageFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { LanguageIdEnum } from './common/constants';
import { IDisposable, languages } from './fillers/monaco-editor-core';
import {
PreprocessCode,
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 +20,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 +118,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
Loading