From 7fceed221764556e0b55b84d595109d0b585b90e Mon Sep 17 00:00:00 2001 From: Hayden Date: Fri, 29 Mar 2024 18:45:22 +0800 Subject: [PATCH] feat: improve ts and export (#112) * feat: improve declaration files * feat: improve tsconfig.json and fix ts check errors * feat: improve languageFeatures * feat: improve debounce types * feat: improve ts type * feat: enhance languageFeatures * feat: languages are loaded on demand * feat: remove useless exports * docs: update website --- src/baseSQLWorker.ts | 10 +-- src/common/utils.ts | 14 ++-- src/editor.worker.d.ts | 3 + src/flinksql/flinkSQLWorker.ts | 3 +- src/flinksql/flinksql.contribution.ts | 4 +- src/flinksql/flinksql.worker.ts | 3 +- src/hivesql/hiveSQLWorker.ts | 3 +- src/hivesql/hivesql.contribution.ts | 4 +- src/hivesql/hivesql.worker.ts | 3 +- src/impalasql/impalaSQLWorker.ts | 3 +- src/impalasql/impalasql.contribution.ts | 4 +- src/impalasql/impalasql.worker.ts | 3 +- src/index.d.ts | 1 - src/languageFeatures.ts | 37 ++++----- src/languageService.ts | 77 +++++++++++++------ src/main.ts | 20 +++-- src/mocha.d.ts | 13 ---- src/mysql/mySQLWorker.ts | 3 +- src/mysql/mysql.contribution.ts | 6 +- src/mysql/mysql.worker.ts | 3 +- src/pgsql/PgSQLWorker.ts | 3 +- src/pgsql/pgsql.contribution.ts | 4 +- src/pgsql/pgsql.worker.ts | 3 +- src/plsql/plSQLWorker.ts | 3 +- src/plsql/plsql.contribution.ts | 6 +- src/plsql/plsql.worker.ts | 3 +- src/setupLanguageMode.ts | 4 +- src/sparksql/sparkSQLWorker.ts | 3 +- src/sparksql/sparksql.contribution.ts | 4 +- src/sparksql/sparksql.worker.ts | 3 +- src/sql/sql.contribution.ts | 4 +- src/sql/sql.worker.ts | 3 +- src/sql/sqlWorker.ts | 3 +- src/test/testRunner.ts | 2 +- src/trinosql/TrinoSQLWorker.ts | 3 +- src/trinosql/trinosql.contribution.ts | 6 +- src/trinosql/trinosql.worker.ts | 3 +- src/workerManager.ts | 26 +++---- tsconfig.json | 12 +-- website/src/extensions/workbench/sidebar.tsx | 8 +- .../languages/helpers/completionService.ts | 16 ++-- 41 files changed, 186 insertions(+), 153 deletions(-) create mode 100644 src/editor.worker.d.ts delete mode 100644 src/index.d.ts delete mode 100644 src/mocha.d.ts diff --git a/src/baseSQLWorker.ts b/src/baseSQLWorker.ts index c242979c..8f891072 100644 --- a/src/baseSQLWorker.ts +++ b/src/baseSQLWorker.ts @@ -17,14 +17,6 @@ export abstract class BaseSQLWorker { return Promise.resolve([]); } - async valid(code: string): Promise { - if (code) { - const result = this.parser.validate(code); - return Promise.resolve(result); - } - return Promise.resolve([]); - } - async parserTreeToString(code: string): Promise { if (code) { const parser = this.parser.createParser(code); @@ -60,7 +52,7 @@ export abstract class BaseSQLWorker { return Promise.resolve([null, null]); } - async getAllEntities(code: string, position: Position): Promise { + async getAllEntities(code: string, position?: Position): Promise { code = code || this.getTextDocument(); if (code) { const allEntities = this.parser.getAllEntities(code, position); diff --git a/src/common/utils.ts b/src/common/utils.ts index 10a34f06..e635b7f5 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -1,15 +1,19 @@ -export function debounce(func: Function, timeout: number, immediate?: boolean) { - let timer: any = null; - return (...args: any) => { +export function debounce unknown>( + func: T, + timeout: number, + immediate?: boolean +): (...args: Parameters) => unknown { + let timer: NodeJS.Timeout | null = null; + return (...args) => { if (timer) { clearTimeout(timer); } if (immediate && !timer) { - func?.(...args); + return func?.(...args); } timer = setTimeout(() => { - clearTimeout(timer); + timer && clearTimeout(timer); timer = null; func?.(...args); }, timeout); diff --git a/src/editor.worker.d.ts b/src/editor.worker.d.ts new file mode 100644 index 00000000..889be288 --- /dev/null +++ b/src/editor.worker.d.ts @@ -0,0 +1,3 @@ +declare module 'monaco-editor/esm/vs/editor/editor.worker.js' { + export function initialize(callback: (ctx: any, createData: any) => any): void; +} diff --git a/src/flinksql/flinkSQLWorker.ts b/src/flinksql/flinkSQLWorker.ts index 3749536a..d651fff4 100644 --- a/src/flinksql/flinkSQLWorker.ts +++ b/src/flinksql/flinkSQLWorker.ts @@ -6,7 +6,8 @@ import { ICreateData } from '../_.contribution'; export class FLinkSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: FlinkSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new FlinkSQL(); diff --git a/src/flinksql/flinksql.contribution.ts b/src/flinksql/flinksql.contribution.ts index d85f6c32..7d9e3767 100644 --- a/src/flinksql/flinksql.contribution.ts +++ b/src/flinksql/flinksql.contribution.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; @@ -14,8 +14,6 @@ registerLanguage({ loader: () => import('./flinksql') }); -loadLanguage(LanguageIdEnum.FLINK); - setupLanguageFeatures({ languageId: LanguageIdEnum.FLINK, completionItems: true, diff --git a/src/flinksql/flinksql.worker.ts b/src/flinksql/flinksql.worker.ts index cdf061a7..2875b8c1 100644 --- a/src/flinksql/flinksql.worker.ts +++ b/src/flinksql/flinksql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { FLinkSQLWorker } from './flinkSQLWorker'; import { ICreateData } from '../_.contribution'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new FLinkSQLWorker(ctx, createData); }); diff --git a/src/hivesql/hiveSQLWorker.ts b/src/hivesql/hiveSQLWorker.ts index 5cc274ee..8054d3c4 100644 --- a/src/hivesql/hiveSQLWorker.ts +++ b/src/hivesql/hiveSQLWorker.ts @@ -6,7 +6,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class HiveSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: HiveSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new HiveSQL(); diff --git a/src/hivesql/hivesql.contribution.ts b/src/hivesql/hivesql.contribution.ts index f9201a7c..4d5c3eb9 100644 --- a/src/hivesql/hivesql.contribution.ts +++ b/src/hivesql/hivesql.contribution.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; @@ -14,8 +14,6 @@ registerLanguage({ loader: () => import('./hivesql') }); -loadLanguage(LanguageIdEnum.HIVE); - setupLanguageFeatures({ languageId: LanguageIdEnum.HIVE, completionItems: true, diff --git a/src/hivesql/hivesql.worker.ts b/src/hivesql/hivesql.worker.ts index d263022e..7d89af94 100644 --- a/src/hivesql/hivesql.worker.ts +++ b/src/hivesql/hivesql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { HiveSQLWorker } from './hiveSQLWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new HiveSQLWorker(ctx, createData); }); diff --git a/src/impalasql/impalaSQLWorker.ts b/src/impalasql/impalaSQLWorker.ts index 578c7252..484f27fb 100644 --- a/src/impalasql/impalaSQLWorker.ts +++ b/src/impalasql/impalaSQLWorker.ts @@ -6,7 +6,8 @@ import { ICreateData } from '../_.contribution'; export class ImpalaSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: ImpalaSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new ImpalaSQL(); diff --git a/src/impalasql/impalasql.contribution.ts b/src/impalasql/impalasql.contribution.ts index cfe0da82..c84c81b3 100644 --- a/src/impalasql/impalasql.contribution.ts +++ b/src/impalasql/impalasql.contribution.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; @@ -14,8 +14,6 @@ registerLanguage({ loader: () => import('./impalasql') }); -loadLanguage(LanguageIdEnum.IMPALA); - setupLanguageFeatures({ languageId: LanguageIdEnum.IMPALA, completionItems: true, diff --git a/src/impalasql/impalasql.worker.ts b/src/impalasql/impalasql.worker.ts index 451fcf9e..008f0221 100644 --- a/src/impalasql/impalasql.worker.ts +++ b/src/impalasql/impalasql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ImpalaSQLWorker } from './impalaSQLWorker'; import { ICreateData } from '../_.contribution'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new ImpalaSQLWorker(ctx, createData); }); diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index 66e7efb5..00000000 --- a/src/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'monaco-editor/esm/vs/editor/editor.worker.js'; diff --git a/src/languageFeatures.ts b/src/languageFeatures.ts index 24f163d2..a1e1f5cc 100644 --- a/src/languageFeatures.ts +++ b/src/languageFeatures.ts @@ -14,7 +14,7 @@ import type { ParseError } from 'dt-sql-parser'; import type { LanguageServiceDefaults, CompletionService, ICompletionItem } from './_.contribution'; export interface WorkerAccessor { - (first: Uri, ...more: Uri[]): Promise; + (...uris: Uri[]): Promise; } export class DiagnosticsAdapter { @@ -24,7 +24,7 @@ export class DiagnosticsAdapter { constructor( private _languageId: string, private _worker: WorkerAccessor, - private _defaults: LanguageServiceDefaults + private readonly _defaults: LanguageServiceDefaults ) { const onModelAdd = (model: editor.IModel): void => { let modeId = model.getLanguageId(); @@ -35,7 +35,7 @@ export class DiagnosticsAdapter { this._listener[model.uri.toString()] = model.onDidChangeContent( debounce(() => { this._doValidate(model.uri, modeId); - }, 600) + }, 500) ); this._doValidate(model.uri, modeId); @@ -61,14 +61,16 @@ export class DiagnosticsAdapter { }) ); - this._defaults.onDidChange((_) => { - editor.getModels().forEach((model) => { - if (model.getLanguageId() === this._languageId) { - onModelRemoved(model); - onModelAdd(model); - } - }); - }); + this._disposables.push( + this._defaults.onDidChange((_) => { + editor.getModels().forEach((model) => { + if (model.getLanguageId() === this._languageId) { + onModelRemoved(model); + onModelAdd(model); + } + }); + }) + ); this._disposables.push({ dispose: () => { @@ -115,7 +117,7 @@ function toSeverity(lsSeverity?: number): MarkerSeverity { } } -function toDiagnostics(resource: Uri, diag: ParseError): editor.IMarkerData { +function toDiagnostics(_resource: Uri, diag: ParseError): editor.IMarkerData { return { severity: toSeverity(), startLineNumber: diag.startLine, @@ -131,11 +133,10 @@ function toDiagnostics(resource: Uri, diag: ParseError): editor.IMarkerData { export class CompletionAdapter implements languages.CompletionItemProvider { - constructor(private readonly _worker: WorkerAccessor, defaults: LanguageServiceDefaults) { - this._defaults = defaults; - } - - private _defaults: LanguageServiceDefaults; + constructor( + private readonly _worker: WorkerAccessor, + private readonly _defaults: LanguageServiceDefaults + ) {} public get triggerCharacters(): string[] { return ['.', ' ']; @@ -145,7 +146,7 @@ export class CompletionAdapter model: editor.IReadOnlyModel, position: Position, context: languages.CompletionContext, - token: CancellationToken + _token: CancellationToken ): Promise { const resource = model.uri; return this._worker(resource) diff --git a/src/languageService.ts b/src/languageService.ts index df8ecc23..e2fad016 100644 --- a/src/languageService.ts +++ b/src/languageService.ts @@ -4,40 +4,73 @@ import { diagnosticDefault, modeConfigurationDefault } from './_.contribution'; -import { WorkerAccessor } from './languageFeatures'; import { WorkerManager } from './workerManager'; import { BaseSQLWorker } from './baseSQLWorker'; +import { Position, Uri, editor } from './fillers/monaco-editor-core'; -type ClientWorker = (...uris: any) => Promise; +export class LanguageService { + private workerClients: Map> = new Map(); -export class LanguageService { - private worker: Map> = new Map(); + public valid(language: string, model: editor.IReadOnlyModel | string) { + const text = typeof model === 'string' ? model : model.getValue(); + const uri = typeof model === 'string' ? void 0 : model.uri; - public valid(language: string, sqlContent: string): Promise { - const clientWorker = this.getClientWorker(language); - return clientWorker(sqlContent).then((worker) => { - return worker.valid(sqlContent); + const clientWorker = this.getClientWorker(language, uri as Uri); + return clientWorker.then((worker) => { + return worker.doValidation(text); }); } - public parserTreeToString(language: string, sqlContent: string): Promise { - const clientWorker = this.getClientWorker(language); - return clientWorker(sqlContent).then((worker) => { - return worker.parserTreeToString(sqlContent); + public parserTreeToString(language: string, model: editor.IReadOnlyModel | string) { + const text = typeof model === 'string' ? model : model.getValue(); + const uri = typeof model === 'string' ? void 0 : model.uri; + + const clientWorker = this.getClientWorker(language, uri as Uri); + return clientWorker.then((worker) => { + return worker.parserTreeToString(text); + }); + } + + public getAllEntities( + language: string, + model: editor.IReadOnlyModel | string, + position?: Position + ) { + const text = typeof model === 'string' ? model : model.getValue(); + const uri = typeof model === 'string' ? void 0 : model.uri; + + const clientWorker = this.getClientWorker(language, uri as Uri); + return clientWorker.then((worker) => { + return worker.getAllEntities(text, position); }); } - private getClientWorker(language: string): ClientWorker { - let existWorker = this.worker.get(language); - if (!existWorker) { - const client = new WorkerManager(this.getLanguageServiceDefault(language)); - const worker: WorkerAccessor = (...uris): Promise => { - return client.getLanguageServiceWorker(...uris); - }; - this.worker.set(language, worker); - return worker; + /** + * Dispose a language service. + * If the language is null, dispose all language services. + */ + public dispose(language?: string): void { + if (language) { + if (this.workerClients.has(language)) { + this.workerClients.get(language)?.dispose(); + this.workerClients.delete(language); + } + } else { + this.workerClients.forEach((client) => { + client.dispose(); + }); + this.workerClients.clear(); + } + } + + private getClientWorker(language: string, ...uri: Uri[]): Promise { + let existClient = this.workerClients.get(language); + if (!existClient) { + const client = new WorkerManager(this.getLanguageServiceDefault(language)); + this.workerClients.set(language, client); + return client.getLanguageServiceWorker(...uri); } - return existWorker; + return existClient.getLanguageServiceWorker(...uri); } private getLanguageServiceDefault(languageId: string): LanguageServiceDefaults { diff --git a/src/main.ts b/src/main.ts index 60106680..91992a18 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,21 @@ export * from './_.contribution'; export * from './languageService'; -export * from './languageFeatures'; -export * from './setupLanguageMode'; export * from './setupLanguageFeatures'; -export * from './workerManager'; -export * from './common/utils'; export * from './common/constants'; export * from './theme'; -export { SyntaxContextType } from 'dt-sql-parser'; +export { + EntityContextType, + StmtContextType, + /** @deprecated use {@link EntityContextType} to instead. */ + SyntaxContextType +} from 'dt-sql-parser'; -export type { WordRange, SyntaxSuggestion, Suggestions, TextSlice } from 'dt-sql-parser'; +export type { + WordRange, + SyntaxSuggestion, + Suggestions, + TextSlice, + ParseError, + EntityContext +} from 'dt-sql-parser'; diff --git a/src/mocha.d.ts b/src/mocha.d.ts deleted file mode 100644 index c9a22b6f..00000000 --- a/src/mocha.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -declare function run(): void; - -declare function suite(name: string, fn: (err?: any) => void): void; -declare function test(name: string, fn: (done: (err?: any) => void) => void): void; -declare function suiteSetup(fn: (done: (err?: any) => void) => void): void; -declare function suiteTeardown(fn: (done: (err?: any) => void) => void): void; -declare function setup(fn: (done: (err?: any) => void) => void): void; -declare function teardown(fn: (done: (err?: any) => void) => void): void; diff --git a/src/mysql/mySQLWorker.ts b/src/mysql/mySQLWorker.ts index f731ef6b..a2284ccb 100644 --- a/src/mysql/mySQLWorker.ts +++ b/src/mysql/mySQLWorker.ts @@ -6,7 +6,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class MySQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: MySQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new MySQL(); diff --git a/src/mysql/mysql.contribution.ts b/src/mysql/mysql.contribution.ts index bfdc0bd2..f327ef49 100644 --- a/src/mysql/mysql.contribution.ts +++ b/src/mysql/mysql.contribution.ts @@ -3,19 +3,17 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; registerLanguage({ id: LanguageIdEnum.MYSQL, - extensions: ['mysql'], + extensions: ['.mysql'], aliases: ['MySQL', 'mysql'], loader: () => import('./mysql') }); -loadLanguage(LanguageIdEnum.MYSQL); - setupLanguageFeatures({ languageId: LanguageIdEnum.MYSQL, completionItems: true, diff --git a/src/mysql/mysql.worker.ts b/src/mysql/mysql.worker.ts index 29a0a32b..f2cba787 100644 --- a/src/mysql/mysql.worker.ts +++ b/src/mysql/mysql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { MySQLWorker } from './mySQLWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new MySQLWorker(ctx, createData); }); diff --git a/src/pgsql/PgSQLWorker.ts b/src/pgsql/PgSQLWorker.ts index 276e26a0..f30686e4 100644 --- a/src/pgsql/PgSQLWorker.ts +++ b/src/pgsql/PgSQLWorker.ts @@ -5,7 +5,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class PgSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: PostgreSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new PostgreSQL(); diff --git a/src/pgsql/pgsql.contribution.ts b/src/pgsql/pgsql.contribution.ts index 3566dc50..ab6f629e 100644 --- a/src/pgsql/pgsql.contribution.ts +++ b/src/pgsql/pgsql.contribution.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; @@ -14,8 +14,6 @@ registerLanguage({ loader: () => import('./pgsql') }); -loadLanguage(LanguageIdEnum.PG); - setupLanguageFeatures({ languageId: LanguageIdEnum.PG, completionItems: true, diff --git a/src/pgsql/pgsql.worker.ts b/src/pgsql/pgsql.worker.ts index 5d99d2a5..5a01664a 100644 --- a/src/pgsql/pgsql.worker.ts +++ b/src/pgsql/pgsql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { PgSQLWorker } from './PgSQLWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new PgSQLWorker(ctx, createData); }); diff --git a/src/plsql/plSQLWorker.ts b/src/plsql/plSQLWorker.ts index dd85d823..932ef805 100644 --- a/src/plsql/plSQLWorker.ts +++ b/src/plsql/plSQLWorker.ts @@ -6,7 +6,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class PLSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: PLSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new PLSQL(); diff --git a/src/plsql/plsql.contribution.ts b/src/plsql/plsql.contribution.ts index 593c1dcd..de4694a4 100644 --- a/src/plsql/plsql.contribution.ts +++ b/src/plsql/plsql.contribution.ts @@ -3,19 +3,17 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; registerLanguage({ id: LanguageIdEnum.PL, - extensions: [], + extensions: ['.plsql'], aliases: ['PLSQL'], loader: () => import('./plsql') }); -loadLanguage(LanguageIdEnum.PL); - setupLanguageFeatures({ languageId: LanguageIdEnum.PL, completionItems: false, diff --git a/src/plsql/plsql.worker.ts b/src/plsql/plsql.worker.ts index cf0733ec..3335fe6c 100644 --- a/src/plsql/plsql.worker.ts +++ b/src/plsql/plsql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { PLSQLWorker } from './plSQLWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new PLSQLWorker(ctx, createData); }); diff --git a/src/setupLanguageMode.ts b/src/setupLanguageMode.ts index 9286ffae..37990718 100644 --- a/src/setupLanguageMode.ts +++ b/src/setupLanguageMode.ts @@ -10,10 +10,10 @@ export function setupLanguageMode( const disposables: IDisposable[] = []; const providers: IDisposable[] = []; - const client = new WorkerManager(defaults); + const client = new WorkerManager(defaults); disposables.push(client); - const worker: languageFeatures.WorkerAccessor = (...uris: Uri[]): Promise => { + const worker: languageFeatures.WorkerAccessor = (...uris: Uri[]) => { return client.getLanguageServiceWorker(...uris); }; diff --git a/src/sparksql/sparkSQLWorker.ts b/src/sparksql/sparkSQLWorker.ts index 6e5bbb75..a7bf744c 100644 --- a/src/sparksql/sparkSQLWorker.ts +++ b/src/sparksql/sparkSQLWorker.ts @@ -6,7 +6,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class SparkSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: SparkSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new SparkSQL(); diff --git a/src/sparksql/sparksql.contribution.ts b/src/sparksql/sparksql.contribution.ts index 7e7e2a2f..9eed46cb 100644 --- a/src/sparksql/sparksql.contribution.ts +++ b/src/sparksql/sparksql.contribution.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; @@ -14,8 +14,6 @@ registerLanguage({ loader: () => import('./sparksql') }); -loadLanguage(LanguageIdEnum.SPARK); - setupLanguageFeatures({ languageId: LanguageIdEnum.SPARK, completionItems: true, diff --git a/src/sparksql/sparksql.worker.ts b/src/sparksql/sparksql.worker.ts index 98732ca6..36f9c6b2 100644 --- a/src/sparksql/sparksql.worker.ts +++ b/src/sparksql/sparksql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { SparkSQLWorker } from './sparkSQLWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { const workerInstance = new SparkSQLWorker(ctx, createData); return workerInstance; diff --git a/src/sql/sql.contribution.ts b/src/sql/sql.contribution.ts index 7460dae2..c9ed8f16 100644 --- a/src/sql/sql.contribution.ts +++ b/src/sql/sql.contribution.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; @@ -14,8 +14,6 @@ registerLanguage({ loader: () => import('./sql') }); -loadLanguage(LanguageIdEnum.SQL); - setupLanguageFeatures({ languageId: LanguageIdEnum.SQL, completionItems: false, diff --git a/src/sql/sql.worker.ts b/src/sql/sql.worker.ts index 7a2ef0a9..8a92e631 100644 --- a/src/sql/sql.worker.ts +++ b/src/sql/sql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { SQLWorker } from './sqlWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new SQLWorker(ctx, createData); }); diff --git a/src/sql/sqlWorker.ts b/src/sql/sqlWorker.ts index 0a8346fc..81f60df8 100644 --- a/src/sql/sqlWorker.ts +++ b/src/sql/sqlWorker.ts @@ -6,7 +6,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class SQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: MySQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new MySQL(); diff --git a/src/test/testRunner.ts b/src/test/testRunner.ts index adfeefc0..0ed147d4 100644 --- a/src/test/testRunner.ts +++ b/src/test/testRunner.ts @@ -19,7 +19,7 @@ export interface ITestItem { } function timeout(ms: number) { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { setTimeout(resolve, ms); }); } diff --git a/src/trinosql/TrinoSQLWorker.ts b/src/trinosql/TrinoSQLWorker.ts index 98e9c75c..d8d0de33 100644 --- a/src/trinosql/TrinoSQLWorker.ts +++ b/src/trinosql/TrinoSQLWorker.ts @@ -6,7 +6,8 @@ import { BaseSQLWorker } from '../baseSQLWorker'; export class TrinoSQLWorker extends BaseSQLWorker { protected _ctx: worker.IWorkerContext; protected parser: TrinoSQL; - constructor(ctx: worker.IWorkerContext, createData: ICreateData) { + constructor(ctx: worker.IWorkerContext, _createData: ICreateData) { + // CreatedData is not required now. super(); this._ctx = ctx; this.parser = new TrinoSQL(); diff --git a/src/trinosql/trinosql.contribution.ts b/src/trinosql/trinosql.contribution.ts index e3bc2f9b..235723eb 100644 --- a/src/trinosql/trinosql.contribution.ts +++ b/src/trinosql/trinosql.contribution.ts @@ -1,16 +1,14 @@ -import { loadLanguage, registerLanguage } from '../_.contribution'; +import { registerLanguage } from '../_.contribution'; import { setupLanguageFeatures } from '../setupLanguageFeatures'; import { LanguageIdEnum } from '../common/constants'; registerLanguage({ id: LanguageIdEnum.TRINO, - extensions: [], + extensions: ['.trinosql'], aliases: ['TrinoSQL', 'trino', 'Trino', 'prestosql', 'PrestoSQL', 'presto', 'Presto'], loader: () => import('./trinosql') }); -loadLanguage(LanguageIdEnum.TRINO); - setupLanguageFeatures({ languageId: LanguageIdEnum.TRINO, completionItems: true, diff --git a/src/trinosql/trinosql.worker.ts b/src/trinosql/trinosql.worker.ts index 4e7a4534..830700a0 100644 --- a/src/trinosql/trinosql.worker.ts +++ b/src/trinosql/trinosql.worker.ts @@ -3,7 +3,8 @@ import * as EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js'; import { ICreateData } from '../_.contribution'; import { TrinoSQLWorker } from './TrinoSQLWorker'; -self.onmessage = (e: any) => { +self.onmessage = () => { + // ignore the first message EditorWorker.initialize((ctx: worker.IWorkerContext, createData: ICreateData) => { return new TrinoSQLWorker(ctx, createData); }); diff --git a/src/workerManager.ts b/src/workerManager.ts index c79a5362..b4b4cc7c 100644 --- a/src/workerManager.ts +++ b/src/workerManager.ts @@ -1,20 +1,22 @@ import { LanguageServiceDefaults } from './_.contribution'; +import { BaseSQLWorker } from './baseSQLWorker'; import { editor, IDisposable, Uri } from './fillers/monaco-editor-core'; const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min -export class WorkerManager { +export class WorkerManager { private _defaults: LanguageServiceDefaults; private _idleCheckInterval: number; private _lastUsedTime: number; private _configChangeListener: IDisposable; private _worker: editor.MonacoWebWorker | null; - private _client: Promise | null | undefined; + private _client: Promise | null; constructor(defaults: LanguageServiceDefaults) { this._defaults = defaults; this._worker = null; + this._client = null; this._idleCheckInterval = window.setInterval(() => this._checkIfIdle(), 30 * 1000); this._lastUsedTime = 0; this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker()); @@ -60,21 +62,19 @@ export class WorkerManager { } }); - this._client = >(this._worker.getProxy()); + this._client = this._worker.getProxy(); } return this._client; } - getLanguageServiceWorker(...resources: Uri[]): Promise { - let _client: T; - return this._getClient() - .then((client) => { - _client = client; - }) - .then((_) => { - return this._worker?.withSyncedResources(resources); - }) - .then((_) => _client); + getLanguageServiceWorker(..._resources: Uri[]): Promise { + const resources = _resources?.filter(Boolean); + return this._getClient().then((client) => { + if (resources && resources?.length && this._worker) { + this._worker.withSyncedResources(resources); + } + return client; + }); } } diff --git a/tsconfig.json b/tsconfig.json index 2fda2288..aee7297c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,19 +1,21 @@ { "compilerOptions": { + "baseUrl": "./", "rootDir": "./src", "target": "es6", "module": "esnext", "lib": ["ES2020", "DOM"], + "noEmit": true, "allowJs": true, "skipLibCheck": true, - "esModuleInterop": true, - "strict": true, - "forceConsistentCasingInFileNames": true, "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, - "noEmit": true, - "baseUrl": "./" + "esModuleInterop": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "noUnusedLocals": true, + "noUnusedParameters": true }, "include": ["src/**/*"] } diff --git a/website/src/extensions/workbench/sidebar.tsx b/website/src/extensions/workbench/sidebar.tsx index 46f54fd7..c8b4d378 100644 --- a/website/src/extensions/workbench/sidebar.tsx +++ b/website/src/extensions/workbench/sidebar.tsx @@ -8,7 +8,7 @@ import { Select, Option } from '@dtinsight/molecule/esm/components/select'; import { IEditorTab, IProblemsItem, MarkerSeverity } from '@dtinsight/molecule/esm/model'; import { defaultLanguage, defaultEditorTab, defaultLanguageStatusItem, languages } from './common'; -import { LanguageService } from 'monaco-sql-languages/out/esm/languageService'; +import { LanguageService, ParseError } from 'monaco-sql-languages/out/esm/languageService'; import { debounce } from 'monaco-sql-languages/out/esm/common/utils'; export default class Sidebar extends React.Component { @@ -49,7 +49,11 @@ export default class Sidebar extends React.Component { }); }, 200); - convertMsgToProblemItem = (tab: IEditorTab, code: string, msgs = []): IProblemsItem => { + convertMsgToProblemItem = ( + tab: IEditorTab, + code: string, + msgs: ParseError[] = [] + ): IProblemsItem => { const rootId = tab.id; const rootName = `${tab.name || ''}`; const languageProblems: IProblemsItem = { diff --git a/website/src/languages/helpers/completionService.ts b/website/src/languages/helpers/completionService.ts index c4f3ec5c..8a69c99b 100644 --- a/website/src/languages/helpers/completionService.ts +++ b/website/src/languages/helpers/completionService.ts @@ -1,6 +1,6 @@ import { languages } from 'monaco-editor/esm/vs/editor/editor.api'; import { CompletionService, ICompletionItem } from 'monaco-sql-languages/out/esm/languageService'; -import { SyntaxContextType } from 'monaco-sql-languages/out/esm/main'; +import { EntityContextType } from 'monaco-sql-languages/out/esm/main'; import { getCatalogs, getDataBases, getSchemas, getTables, getViews } from './dbMetaProvider'; @@ -59,8 +59,8 @@ export const completionService: CompletionService = async function ( const wordCount = words.length; if ( - syntaxContextType === SyntaxContextType.CATALOG || - syntaxContextType === SyntaxContextType.DATABASE_CREATE + syntaxContextType === EntityContextType.CATALOG || + syntaxContextType === EntityContextType.DATABASE_CREATE ) { if (!existCatalogCompletions && wordCount <= 1) { syntaxCompletionItems = syntaxCompletionItems.concat(await getCatalogs(languageId)); @@ -69,9 +69,9 @@ export const completionService: CompletionService = async function ( } if ( - syntaxContextType === SyntaxContextType.DATABASE || - syntaxContextType === SyntaxContextType.TABLE_CREATE || - syntaxContextType === SyntaxContextType.VIEW_CREATE + syntaxContextType === EntityContextType.DATABASE || + syntaxContextType === EntityContextType.TABLE_CREATE || + syntaxContextType === EntityContextType.VIEW_CREATE ) { if (!existCatalogCompletions && haveCatalog && wordCount <= 1) { syntaxCompletionItems = syntaxCompletionItems.concat(await getCatalogs(languageId)); @@ -92,7 +92,7 @@ export const completionService: CompletionService = async function ( } } - if (syntaxContextType === SyntaxContextType.TABLE) { + if (syntaxContextType === EntityContextType.TABLE) { if (wordCount <= 1) { if (!existCatalogCompletions && haveCatalog) { const ctas = await getCatalogs(languageId); @@ -137,7 +137,7 @@ export const completionService: CompletionService = async function ( } } - if (syntaxContextType === SyntaxContextType.VIEW) { + if (syntaxContextType === EntityContextType.VIEW) { if (wordCount <= 1) { if (!existCatalogCompletions && haveCatalog) { syntaxCompletionItems = syntaxCompletionItems.concat(