diff --git a/src/server/languageProvider.ts b/src/server/languageProvider.ts index 6667608..58a431d 100644 --- a/src/server/languageProvider.ts +++ b/src/server/languageProvider.ts @@ -166,13 +166,22 @@ export default class LanguageProvider { } public handles(resource: string, doc: TextDocument): boolean { - if (doc && this.description.modeIds.indexOf(doc.languageId) >= 0) { + if (doc && this.description.modeIds.includes(doc.languageId)) { return true } - const base = path.basename(Uri.parse(resource).fsPath) + return this.handlesConfigFile(Uri.parse(resource)) + } + + private handlesConfigFile(uri: Uri): boolean { + const base = path.basename(uri.fsPath) return !!base && (!!this.description.configFilePattern && this.description.configFilePattern.test(base)) } + public handlesUri(resource: Uri): boolean { + const ext = path.extname(resource.path).slice(1).toLowerCase() + return this.description.standardFileExtensions.includes(ext) || this.handlesConfigFile(resource) + } + private get id(): string { // tslint:disable-line return this.description.id } diff --git a/src/server/typescriptServiceClientHost.ts b/src/server/typescriptServiceClientHost.ts index 0305b25..9b575fe 100644 --- a/src/server/typescriptServiceClientHost.ts +++ b/src/server/typescriptServiceClientHost.ts @@ -2,7 +2,7 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { disposeAll, languages, TextDocument, Uri, workspace } from 'coc.nvim' +import { ConfigurationChangeEvent, disposeAll, languages, TextDocument, Uri, workspace } from 'coc.nvim' import { CancellationToken, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, Disposable, Position, Range } from 'vscode-languageserver-protocol' import { flatten } from '../utils/arrays' import { PluginManager } from '../utils/plugins' @@ -113,6 +113,7 @@ export default class TypeScriptServiceClientHost implements Disposable { diagnosticSource: 'ts-plugin', diagnosticLanguage: DiagnosticLanguage.TypeScript, diagnosticOwner: 'typescript', + standardFileExtensions: [], isExternal: true }) } else { @@ -129,6 +130,7 @@ export default class TypeScriptServiceClientHost implements Disposable { diagnosticSource: 'ts-plugin', diagnosticLanguage: DiagnosticLanguage.TypeScript, diagnosticOwner: 'typescript', + standardFileExtensions: [], isExternal: true }) } @@ -176,17 +178,20 @@ export default class TypeScriptServiceClientHost implements Disposable { return this.languagePerId.get(languageId) } - private configurationChanged(): void { - const config = workspace.getConfiguration('tsserver') - this.reportStyleCheckAsWarnings = config.get('reportStyleChecksAsWarnings', true) + private configurationChanged(e?: ConfigurationChangeEvent): void { + if (!e || e.affectsConfiguration('tsserver')) { + const config = workspace.getConfiguration('tsserver') + this.reportStyleCheckAsWarnings = config.get('reportStyleChecksAsWarnings', true) + } } public async findLanguage(uri: string): Promise { try { let doc = this.client.getDocument(uri) - if (!doc) return undefined let languages = Array.from(this.languagePerId.values()) - return languages.find(language => language.handles(uri, doc.textDocument)) + // possible not opened + if (doc) return languages.find(language => language.handles(uri, doc.textDocument)) + return languages.find(language => language.handlesUri(Uri.parse(uri))) } catch { return undefined } diff --git a/src/server/utils/languageDescription.ts b/src/server/utils/languageDescription.ts index 75cd1f9..c3a09af 100644 --- a/src/server/utils/languageDescription.ts +++ b/src/server/utils/languageDescription.ts @@ -13,6 +13,7 @@ export interface LanguageDescription { readonly isExternal?: boolean readonly diagnosticOwner: string readonly configFilePattern?: RegExp + readonly standardFileExtensions: ReadonlyArray, } export const enum DiagnosticLanguage { @@ -29,7 +30,13 @@ export const standardLanguageDescriptions: LanguageDescription[] = [ languageModeIds.typescripttsx, languageModeIds.typescriptjsx], diagnosticLanguage: DiagnosticLanguage.TypeScript, configFile: 'tsconfig.json', - configFilePattern: /^tsconfig(\..*)?\.json$/gi + configFilePattern: /^tsconfig(\..*)?\.json$/gi, + standardFileExtensions: [ + 'ts', + 'tsx', + 'cts', + 'mts' + ] }, { id: 'javascript', @@ -38,6 +45,14 @@ export const standardLanguageDescriptions: LanguageDescription[] = [ modeIds: [languageModeIds.javascript, languageModeIds.javascriptreact, languageModeIds.javascriptjsx], diagnosticLanguage: DiagnosticLanguage.JavaScript, configFile: 'jsconfig.json', - configFilePattern: /^jsconfig(\..*)?\.json$/gi + configFilePattern: /^jsconfig(\..*)?\.json$/gi, + standardFileExtensions: [ + 'js', + 'jsx', + 'cjs', + 'mjs', + 'es6', + 'pac', + ] } ]