fix organizeImports not working sometimes

This commit is contained in:
chemzqm 2018-10-25 22:47:00 +08:00
parent 68a9e822e9
commit e4dbb9f68c
5 changed files with 106 additions and 94 deletions

View file

@ -379,15 +379,15 @@
"author": "chemzqm@gmail.com",
"license": "MIT",
"devDependencies": {
"@types/fast-diff": "^1.1.0",
"@chemzqm/tsconfig": "^0.0.3",
"@chemzqm/tslint-config": "^1.0.17",
"@types/node": "^10.11.7",
"coc.nvim": "^0.0.23",
"@types/node": "^10.12.0",
"coc.nvim": "^0.0.28",
"rimraf": "^2.6.2",
"tslint": "^5.11.0"
},
"dependencies": {
"@types/fast-diff": "^1.1.0",
"fast-diff": "^1.2.0",
"semver": "^5.6.0",
"tslib": "^1.9.3",
@ -397,3 +397,4 @@
"which": "^1.3.1"
}
}

View file

@ -1,6 +1,7 @@
import { commands, ExtensionContext, services, workspace } from 'coc.nvim'
import TsserverService from './server'
import { Command, OpenTsServerLogCommand, ReloadProjectsCommand, TypeScriptGoToProjectConfigCommand } from './server/commands'
import OrganizeImportsCommand from './server/organizeImports'
export async function activate(context: ExtensionContext): Promise<void> {
let { subscriptions } = context
@ -24,6 +25,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
registCommand(new ReloadProjectsCommand(service.clientHost))
registCommand(new OpenTsServerLogCommand(service.clientHost))
registCommand(new TypeScriptGoToProjectConfigCommand(service.clientHost))
registCommand(new OrganizeImportsCommand(service.clientHost))
registCommand(commands.register({
id: 'tsserver.restart',
execute: (): void => {

View file

@ -18,7 +18,6 @@ import Folding from './features/folding'
import FormattingProvider from './features/formatting'
import HoverProvider from './features/hover'
import ImplementationsCodeLensProvider from './features/implementationsCodeLens'
import OrganizeImportsProvider from './features/organizeImports'
// import TagCompletionProvider from './features/tagCompletion'
import QuickfixProvider from './features/quickfix'
import RefactorProvider from './features/refactor'
@ -213,10 +212,6 @@ export default class LanguageProvider {
// )
if (this.client.apiVersion.gte(API.v280)) {
this.disposables.push(
new OrganizeImportsProvider(client, commands, this.fileConfigurationManager, this.description.id)
)
this.disposables.push(
languages.registerFoldingRangeProvider(languageIds, new Folding(this.client))
)

View file

@ -2,24 +2,22 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { disposeAll, TextDocumentWillSaveEvent, workspace } from 'coc.nvim'
import { Command, CommandManager } from 'coc.nvim/lib/commands'
import { Disposable, TextDocument, TextEdit, WorkspaceEdit } from 'vscode-languageserver-protocol'
import Proto from '../protocol'
import { ITypeScriptServiceClient } from '../typescriptService'
import { standardLanguageDescriptions } from '../utils/languageDescription'
import * as typeconverts from '../utils/typeConverters'
import FileConfigurationManager from './fileConfigurationManager'
import { TextDocumentWillSaveEvent, workspace } from 'coc.nvim'
import { TextDocument, TextEdit, WorkspaceEdit } from 'vscode-languageserver-protocol'
import { Command } from './commands'
import Proto from './protocol'
import TypeScriptServiceClientHost from './typescriptServiceClientHost'
import { standardLanguageDescriptions } from './utils/languageDescription'
import { languageIds } from './utils/languageModeIds'
import * as typeconverts from './utils/typeConverters'
class OrganizeImportsCommand implements Command {
export default class OrganizeImportsCommand implements Command {
public readonly id: string = 'tsserver.organizeImports'
constructor(
private readonly client: ITypeScriptServiceClient,
private noSemicolons: boolean,
private modeIds: string[]
private readonly client: TypeScriptServiceClientHost
) {
workspace.onWillSaveUntil(this.onWillSaveUntil, this, 'tsserver-organizeImports')
workspace.onWillSaveUntil(this.onWillSaveUntil, this, 'tsserver')
}
private onWillSaveUntil(event: TextDocumentWillSaveEvent): void {
@ -27,7 +25,7 @@ class OrganizeImportsCommand implements Command {
let format = config.get('orgnizeImportOnSave', false)
if (!format) return
let { document } = event
if (this.modeIds.indexOf(document.languageId) == -1) return
if (languageIds.indexOf(document.languageId) == -1) return
let willSaveWaitUntil = async (): Promise<TextEdit[]> => {
let edit = await this.getTextEdits(document)
if (!edit) return []
@ -37,7 +35,8 @@ class OrganizeImportsCommand implements Command {
}
private async getTextEdits(document: TextDocument): Promise<WorkspaceEdit | null> {
let file = this.client.toPath(document.uri)
let client = this.client.serviceClient
let file = client.toPath(document.uri)
const args: Proto.OrganizeImportsRequestArgs = {
scope: {
type: 'file',
@ -46,16 +45,21 @@ class OrganizeImportsCommand implements Command {
}
}
}
const response = await this.client.execute('organizeImports', args)
const response = await client.execute('organizeImports', args)
if (!response || !response.success) {
return
}
const edit = typeconverts.WorkspaceEdit.fromFileCodeEdits(
this.client,
client,
response.body
)
if (this.noSemicolons) {
let desc = standardLanguageDescriptions.find(o => o.modeIds.indexOf(document.languageId) !== -1)
if (!desc) return null
const config = workspace.getConfiguration(`${desc.id}.preferences`)
let noSemicolons = config.get<boolean>('noSemicolons', false)
if (noSemicolons) {
let { changes } = edit
if (changes) {
for (let c of Object.keys(changes)) {
@ -70,32 +74,9 @@ class OrganizeImportsCommand implements Command {
public async execute(): Promise<void> {
let document = await workspace.document
if (this.modeIds.indexOf(document.filetype) == -1) return
if (languageIds.indexOf(document.filetype) == -1) return
let edit = await this.getTextEdits(document.textDocument)
if (edit) await workspace.applyEdit(edit)
return
}
}
export default class OrganizeImports {
private disposables: Disposable[] = []
public constructor(
client: ITypeScriptServiceClient,
commandManager: CommandManager,
fileConfigurationManager: FileConfigurationManager,
languageId: string
) {
let description = standardLanguageDescriptions.find(o => o.id == languageId)
let modeIds = description ? description.modeIds : []
let noSemicolons = fileConfigurationManager.removeSemicolons(languageId)
let cmd = new OrganizeImportsCommand(client, noSemicolons, modeIds)
commandManager.register(cmd)
this.disposables.push(Disposable.create(() => {
commandManager.unregister(cmd.id)
}))
}
public dispose(): void {
disposeAll(this.disposables)
}
}

121
yarn.lock
View file

@ -87,10 +87,10 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"
"@chemzqm/neovim@4.3.23":
version "4.3.23"
resolved "https://registry.yarnpkg.com/@chemzqm/neovim/-/neovim-4.3.23.tgz#85385db1bedde01593be5f5ea7cbf7db10a868aa"
integrity sha512-NVg29qZE5x6x3KZWaOHYCA7umpikxhnHc4fwFl9SaPpJ/2rKrEGUZhKSXWJstt86uSIvjpicD84OMHi3zV8LZg==
"@chemzqm/neovim@4.3.24":
version "4.3.24"
resolved "https://registry.yarnpkg.com/@chemzqm/neovim/-/neovim-4.3.24.tgz#2c571b3dae6fcdfb81a648d9048db914a9d28f4b"
integrity sha512-10DesU4DjTZ/cwoBA8QyJfrn/6smyup5446xwLJBETTampM2JMtOvSKIubSjUbR9xiLyUbpV58COb9uv7PAtZQ==
dependencies:
babel-eslint "^8.2.6"
msgpack-lite "^0.1.26"
@ -114,17 +114,10 @@
resolved "https://registry.yarnpkg.com/@types/fast-diff/-/fast-diff-1.1.0.tgz#68c7f476025740b0b6756e51e38b1188dd528b0e"
integrity sha512-doBwHnPAGdE54EiIFc0/v1qHlRVtNTaxgCZOS9SdSGPq91vx3kUKi5fmz/yivW/RAXEOUJYVXtVFQ6IPpqKRWg==
"@types/mkdirp@^0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.5.2.tgz#503aacfe5cc2703d5484326b1b27efa67a339c1f"
integrity sha512-U5icWpv7YnZYGsN4/cmh3WD2onMY0aJIiTE6+51TwJCttdHvtCYmkBNOobHlXwrJRL0nkH9jH4kD+1FAdMN4Tg==
dependencies:
"@types/node" "*"
"@types/node@*", "@types/node@^10.11.7":
version "10.11.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.11.7.tgz#0e75ca9357d646ca754016ca1d68a127ad7e7300"
integrity sha512-yOxFfkN9xUFLyvWaeYj90mlqTJ41CsQzWKS3gXdOMOyPVacUsymejKxJ4/pMW7exouubuEeZLJawGgcNGYlTeg==
"@types/node@^10.12.0":
version "10.12.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.0.tgz#ea6dcbddbc5b584c83f06c60e82736d8fbb0c235"
integrity sha512-3TUHC3jsBAB7qVRGxT6lWyYo2v96BMmD2PTcl47H25Lu7UXtFH/2qqmKiVrnel6Ne//0TFYf6uvNX+HW2FRkLQ==
ansi-regex@^2.0.0:
version "2.1.1"
@ -226,28 +219,27 @@ circular-json@^0.5.5:
resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.5.7.tgz#b8be478d72ea58c7eeda26bf1cf1fba43d188842"
integrity sha512-/pXoV1JA847qRKPrHbBK6YIBGFF8GOP4wzSgUOA7q0ew0vAv0iJswP+2/nZQ9uzA3Azi7eTrg9L2yzXc/7ZMIA==
coc.nvim@^0.0.23:
version "0.0.23"
resolved "https://registry.yarnpkg.com/coc.nvim/-/coc.nvim-0.0.23.tgz#8fe40852fa505f152ac42a5303706caf16e51572"
integrity sha512-AGvabmtzqVsQBqwuwMqRS4LK+wtt8B7UFZ1aqJ/Dkl9DBN7YS1EeyJf3MrERxYq3nV7Hm6K5ZjdNbJ6TNJb/fw==
coc.nvim@^0.0.28:
version "0.0.28"
resolved "https://registry.yarnpkg.com/coc.nvim/-/coc.nvim-0.0.28.tgz#5078d1269d8ab79ece6d7bb8aa4df041b71c1ba9"
integrity sha512-OmJEuiJL/Otpp8h6t/Ck5SxXL5LMIlDDZpLlqe2HC3QyK0e65BgJEBo0mwtoeTGPD5WGusn99yhQT0Fjv3bk5w==
dependencies:
"@chemzqm/neovim" "4.3.23"
"@types/mkdirp" "^0.5.2"
"@chemzqm/neovim" "4.3.24"
debounce "^1.2.0"
deep-equal "^1.0.1"
diff "^3.5.0"
fast-diff "^1.1.2"
fast-diff "^1.2.0"
fb-watchman "^2.0.0"
find-up "^3.0.0"
fuzzaldrin "^2.1.0"
glob "^7.1.3"
isuri "^2.0.3"
jsonc-parser "^2.0.2"
log4js "^3.0.5"
log4js "^3.0.6"
minimatch "^3.0.4"
mkdirp "^0.5.1"
node-json-db "^0.9.0"
node-json-db "^0.9.1"
node-serial "^0.1.1"
pify "^4.0.0"
semver "^5.5.1"
semver "^5.6.0"
tslib "^1.9.3"
uuid "^3.3.2"
vscode-languageserver-protocol "^3.13.0"
@ -300,12 +292,7 @@ debug@^3.1.0:
dependencies:
ms "^2.1.1"
deep-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
integrity sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=
diff@^3.2.0, diff@^3.5.0:
diff@^3.2.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==
@ -355,11 +342,6 @@ event-lite@^0.1.1:
resolved "https://registry.yarnpkg.com/event-lite/-/event-lite-0.1.2.tgz#838a3e0fdddef8cc90f128006c8e55a4e4e4c11b"
integrity sha512-HnSYx1BsJ87/p6swwzv+2v6B4X+uxUteoDfRxsAb1S1BePzQqOLevVmkdA15GHJVd9A9Ok6wygUR18Hu0YeV9g==
fast-diff@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.1.2.tgz#4b62c42b8e03de3f848460b639079920695d0154"
integrity sha512-KaJUt+M9t1qaIteSvjc6P3RbMdXsNhK61GRftR6SNxqmhthcd9MGIi4T+o0jD8LUSpSnSKXE20nLtJ3fOHxQig==
fast-diff@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
@ -372,6 +354,13 @@ fb-watchman@^2.0.0:
dependencies:
bser "^2.0.0"
find-up@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
integrity sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==
dependencies:
locate-path "^3.0.0"
fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
@ -451,6 +440,13 @@ isexe@^2.0.0:
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
isuri@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/isuri/-/isuri-2.0.3.tgz#3437121db2fe65af0ba080b7e1a8636f632cca91"
integrity sha1-NDcSHbL+Za8LoIC34ahjb2MsypE=
dependencies:
rfc-3986 "1.0.1"
js-tokens@^3.0.0, js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
@ -479,15 +475,23 @@ jsonc-parser@^2.0.2:
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.0.2.tgz#42fcf56d70852a043fadafde51ddb4a85649978d"
integrity sha512-TSU435K5tEKh3g7bam1AFf+uZrISheoDsLlpmAo6wWZYqjsnd09lHYK1Qo+moK4Ikifev1Gdpa69g4NELKnCrQ==
locate-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
integrity sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==
dependencies:
p-locate "^3.0.0"
path-exists "^3.0.0"
lodash@^4.2.0:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
log4js@^3.0.5:
version "3.0.5"
resolved "https://registry.yarnpkg.com/log4js/-/log4js-3.0.5.tgz#b80146bfebad68b430d4f3569556d8a6edfef303"
integrity sha512-IX5c3G/7fuTtdr0JjOT2OIR12aTESVhsH6cEsijloYwKgcPRlO6DgOU72v0UFhWcoV1HN6+M3dwT89qVPLXm0w==
log4js@^3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/log4js/-/log4js-3.0.6.tgz#e6caced94967eeeb9ce399f9f8682a4b2b28c8ff"
integrity sha512-ezXZk6oPJCWL483zj64pNkMuY/NcRX5MPiB0zE6tjZM137aeusrOnW1ecxgF9cmwMWkBMhjteQxBPoZBh9FDxQ==
dependencies:
circular-json "^0.5.5"
date-format "^1.2.0"
@ -546,7 +550,7 @@ node-int64@^0.4.0:
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
integrity sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs=
node-json-db@^0.9.0:
node-json-db@^0.9.1:
version "0.9.1"
resolved "https://registry.yarnpkg.com/node-json-db/-/node-json-db-0.9.1.tgz#f6fe9b156f1f999add9bdf19a2056ed524e15deb"
integrity sha512-4BydUI7a10W8QBdHq/J3UBswU1i8WhCgTS4BZU0MjlUKrSU7cuUti71eojistgqe5hIrb4adj/wvAT5dw63NPg==
@ -567,6 +571,30 @@ once@^1.3.0:
dependencies:
wrappy "1"
p-limit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.0.0.tgz#e624ed54ee8c460a778b3c9f3670496ff8a57aec"
integrity sha512-fl5s52lI5ahKCernzzIyAP0QAZbGIovtVHGwpcu1Jr/EpzLVDI2myISHwGqK7m8uQFugVWSrbxH7XnhGtvEc+A==
dependencies:
p-try "^2.0.0"
p-locate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4"
integrity sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==
dependencies:
p-limit "^2.0.0"
p-try@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.0.0.tgz#85080bb87c64688fa47996fe8f7dfbe8211760b1"
integrity sha512-hMp0onDKIajHfIkdRk3P4CdCmErkYAxxDtP3Wx/4nZ3aGlau2VKh3mZpcuFkH27WQkL/3WBCPOktzA9ZOAnMQQ==
path-exists@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"
integrity sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=
path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
@ -607,6 +635,11 @@ resolve@^1.3.2:
dependencies:
path-parse "^1.0.5"
rfc-3986@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/rfc-3986/-/rfc-3986-1.0.1.tgz#eeeb88342fadbe8027c0f36ada921a13e6f96206"
integrity sha1-7uuINC+tvoAnwPNq2pIaE+b5YgY=
rfdc@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/rfdc/-/rfdc-1.1.2.tgz#e6e72d74f5dc39de8f538f65e00c36c18018e349"
@ -624,7 +657,7 @@ safe-buffer@~5.1.0, safe-buffer@~5.1.1:
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
semver@^5.3.0, semver@^5.5.1:
semver@^5.3.0:
version "5.5.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477"
integrity sha512-PqpAxfrEhlSUWge8dwIp4tZnQ25DIOthpiaHNIthsjEFQD6EvqUKUDM7L8O2rShkFccYo1VjJR0coWfNkCubRw==