fix suggestionActions.enabled option not working

This commit is contained in:
chemzqm 2019-06-11 17:36:41 +08:00
parent bda7301e8b
commit c3d2bc4d4d
3 changed files with 37 additions and 27 deletions

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Diagnostic } from 'vscode-languageserver-protocol'
import { languages, DiagnosticCollection } from 'coc.nvim'
import { workspace, languages, DiagnosticCollection } from 'coc.nvim'
import { ResourceMap } from './resourceMap'
export class DiagnosticSet {
@ -38,8 +38,8 @@ export class DiagnosticsManager {
private readonly _diagnostics = new Map<DiagnosticKind, DiagnosticSet>()
private readonly _currentDiagnostics: DiagnosticCollection
private _pendingUpdates = new ResourceMap<any>()
private _validate = true
private _enableSuggestions = true
private _enableJavascriptSuggestions = true
private _enableTypescriptSuggestions = true
private readonly updateDelay = 200
@ -65,25 +65,15 @@ export class DiagnosticsManager {
}
}
public set validate(value: boolean) {
if (this._validate === value) {
public setEnableSuggestions(languageId: string, value: boolean): void {
let curr = languageId == 'javascript' ? this._enableJavascriptSuggestions : this._enableTypescriptSuggestions
if (curr == value) {
return
}
this._validate = value
if (!value) {
this._currentDiagnostics.clear()
}
}
public set enableSuggestions(value: boolean) {
if (this._enableSuggestions === value) {
return
}
this._enableSuggestions = value
if (!value) {
this._currentDiagnostics.clear()
if (languageId == 'javascript') {
this._enableJavascriptSuggestions = value
} else {
this._enableTypescriptSuggestions = value
}
}
@ -139,10 +129,6 @@ export class DiagnosticsManager {
this._pendingUpdates.delete(uri)
}
if (!this._validate) {
return
}
const allDiagnostics = [
...this._diagnostics.get(DiagnosticKind.Syntax)!.get(uri),
...this._diagnostics.get(DiagnosticKind.Semantic)!.get(uri),
@ -152,15 +138,28 @@ export class DiagnosticsManager {
}
private getSuggestionDiagnostics(uri: string): Diagnostic[] {
const enabled = this.suggestionsEnabled(uri)
return this._diagnostics
.get(DiagnosticKind.Suggestion)!
.get(uri)
.filter(x => {
if (!this._enableSuggestions) {
if (!enabled) {
// Still show unused
return x.code && x.code == 6133
return x.tags && x.tags.includes(1)
}
return true
return enabled
})
}
private suggestionsEnabled(uri: string): boolean {
let doc = workspace.getDocument(uri)
if (!doc) return false
if (doc.filetype.startsWith('javascript')) {
return this._enableJavascriptSuggestions
}
if (doc.filetype.startsWith('typescript')) {
return this._enableTypescriptSuggestions
}
return true
}
}

View file

@ -34,6 +34,8 @@ import { LanguageDescription } from './utils/languageDescription'
import TypingsStatus from './utils/typingsStatus'
import { OrganizeImportsCodeActionProvider } from './organizeImports'
const suggestionSetting = 'suggestionActions.enabled'
export default class LanguageProvider {
public readonly fileConfigurationManager: FileConfigurationManager // tslint:disable-line
private readonly disposables: Disposable[] = []
@ -45,6 +47,9 @@ export default class LanguageProvider {
) {
this.fileConfigurationManager = new FileConfigurationManager(client)
workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables)
this.configurationChanged()
events.on('BufEnter', bufnr => {
let doc = workspace.getDocument(bufnr)
if (!doc || client.state !== ServiceStat.Running) return
@ -69,6 +74,11 @@ export default class LanguageProvider {
})
}
private configurationChanged(): void {
const config = workspace.getConfiguration(this.id, null)
this.client.diagnosticsManager.setEnableSuggestions(this.id, config.get(suggestionSetting, true))
}
public dispose(): void {
disposeAll(this.disposables)
}

View file

@ -92,6 +92,7 @@ export default class TypeScriptServiceClientHost implements Disposable {
this.client.onTsServerStarted(() => {
this.triggerAllDiagnostics()
})
workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables)
this.configurationChanged()
}