fix diagnostics not works with unopened document

This commit is contained in:
Qiming Zhao 2021-12-25 18:17:07 +08:00
parent ca715ce092
commit 8f96c72ad3
No known key found for this signature in database
GPG key ID: 9722CD0E8D4DCB8C
3 changed files with 39 additions and 10 deletions

View file

@ -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
}

View file

@ -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<LanguageProvider> {
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
}

View file

@ -13,6 +13,7 @@ export interface LanguageDescription {
readonly isExternal?: boolean
readonly diagnosticOwner: string
readonly configFilePattern?: RegExp
readonly standardFileExtensions: ReadonlyArray<string>,
}
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',
]
}
]