Skip to content

Commit

Permalink
use next coc.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Dec 16, 2020
1 parent 3b1e611 commit eaa4c93
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 250 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@
"@chemzqm/tsconfig": "^0.0.3",
"@chemzqm/tslint-config": "^1.0.18",
"@types/node": "^11.13.10",
"coc.nvim": "^0.0.66",
"coc.nvim": "^0.0.79-next.12",
"rimraf": "^2.6.3",
"ts-loader": "^6.0.3",
"tslint": "^5.16.0",
"typescript": "^3.4.4",
"typescript": "^4.1.3",
"vscode-languageserver-protocol": "^3.15.0-next.4",
"webpack": "^4.34.0",
"webpack-cli": "^3.3.4"
Expand Down
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict'
import * as child_process from 'child_process'
import { commands, Terminal, languages, ExtensionContext, LanguageClient, LanguageClientOptions, ServerOptions, services, Uri, workspace, OutputChannel } from 'coc.nvim'
import {
commands,
ExtensionContext,
LanguageClient, LanguageClientOptions, languages, OutputChannel, ServerOptions, services, Terminal, Uri, window, workspace
} from 'coc.nvim'
import * as fs from 'fs'
import os from 'os'
import path from 'path'
import { NotificationType, WorkspaceFolder } from 'vscode-languageserver-protocol'
import { RLSConfiguration } from './configuration'
import { ensureToolchain, rustupUpdate, ensureComponents } from './rustup'
import SignatureHelpProvider from './providers/signatureHelpProvider'
import { ensureComponents, ensureToolchain, rustupUpdate } from './rustup'
import { startSpinner, stopSpinner } from './spinner'
import { ExecChildProcessResult, execFile } from './utils/child_process'
import SignatureHelpProvider from './providers/signatureHelpProvider'

let client: ClientWorkspace
export async function activate(context: ExtensionContext): Promise<void> {
Expand All @@ -18,7 +21,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
let folder = Uri.parse(workspaceFolder.uri).fsPath
return fs.existsSync(path.join(folder, 'Cargo.toml'))
})
let channel = workspace.createOutputChannel('rls')
let channel = window.createOutputChannel('rls')
if (!workspaceFolder) {
channel.appendLine(`[Warning]: A Cargo.toml file must be at the root of the workspace in order to support all features`)
}
Expand Down Expand Up @@ -220,7 +223,7 @@ class ClientWorkspace {
} catch (e) {
// tslint:disable-next-line: no-console
console.error('Error reading sysroot (second try)', e)
workspace.showMessage(`Error reading sysroot: ${e.message}`, 'error')
window.showMessage(`Error reading sysroot: ${e.message}`, 'error')
return env
}
}
Expand Down Expand Up @@ -265,7 +268,7 @@ class ClientWorkspace {
childProcess.on('error', (err: { code?: string; message: string }) => {
if (err.code === 'ENOENT') {
stopSpinner('RLS could not be started')
workspace.showMessage(`Could not spawn RLS: ${err.message}`, 'error')
window.showMessage(`Could not spawn RLS: ${err.message}`, 'error')
this.channel.appendLine(`Could not spawn RLS: ${err.message}`)
}
})
Expand Down
26 changes: 13 additions & 13 deletions src/rustup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as child_process from 'child_process'
import { window, workspace } from 'coc.nvim'
import { startSpinner, stopSpinner } from './spinner'
import { execChildProcess } from './utils/child_process'
'use strict'

import * as child_process from 'child_process'
import { workspace } from 'coc.nvim'

import { execChildProcess } from './utils/child_process'
import { startSpinner, stopSpinner } from './spinner'

const REQUIRED_COMPONENTS = ['rust-analysis', 'rust-src', 'rls']

Expand Down Expand Up @@ -54,7 +54,7 @@ export async function ensureToolchain(config: RustupConfig): Promise<void> {
return
}

const confirmed = await workspace.showPrompt(config.channel + ' toolchain not installed. Install?')
const confirmed = await window.showPrompt(config.channel + ' toolchain not installed. Install?')
if (confirmed) {
await tryToInstallToolchain(config)
}
Expand All @@ -73,15 +73,15 @@ async function hasToolchain(config: RustupConfig): Promise<boolean> {
// tslint:disable-next-line: no-console
console.log(e)
// rustup not present
workspace.showMessage('Rustup not available. Install from https://www.rustup.rs/', 'error')
window.showMessage('Rustup not available. Install from https://www.rustup.rs/', 'error')
throw e
}
}

async function tryToInstallToolchain(config: RustupConfig): Promise<void> {
startSpinner('RLS', 'Installing toolchain…')
try {
let res = await workspace.runTerminalCommand(config.path + ' toolchain install ' + config.channel, workspace.rootPath)
let res = await window.runTerminalCommand(config.path + ' toolchain install ' + config.channel, workspace.rootPath)
if (res.success == false) {
throw new Error(`Install toolchain failed`)
}
Expand All @@ -90,7 +90,7 @@ async function tryToInstallToolchain(config: RustupConfig): Promise<void> {
catch (e) {
// tslint:disable-next-line: no-console
console.error(e)
workspace.showMessage('Could not install ' + config.channel + ' toolchain', 'error')
window.showMessage('Could not install ' + config.channel + ' toolchain', 'error')
stopSpinner('Could not install ' + config.channel + ' toolchain')
throw e
}
Expand All @@ -108,7 +108,7 @@ async function hasRlsComponents(config: RustupConfig): Promise<boolean> {
// tslint:disable-next-line: no-console
console.error(e)
// rustup error?
workspace.showMessage('Unexpected error initialising RLS - error running rustup', 'error')
window.showMessage('Unexpected error initialising RLS - error running rustup', 'error')
throw e
}
}
Expand All @@ -121,10 +121,10 @@ export async function ensureComponents(config: RustupConfig) {
if (await hasRlsComponents(config)) {
return
}
let res = await workspace.showPrompt('Some Rust components not installed. Install?')
let res = await window.showPrompt('Some Rust components not installed. Install?')
if (res) {
await installComponents(config)
workspace.showMessage(`Rust components successfully installed!`, 'more')
window.showMessage(`Rust components successfully installed!`, 'more')
} else {
throw new Error()
}
Expand All @@ -134,7 +134,7 @@ async function installComponents(config: RustupConfig): Promise<void> {
startSpinner('RLS', 'Installing components…')
let install = async component => {
let cmd = config.path + ` component add ${component} --toolchain ` + config.channel
let res = await workspace.runTerminalCommand(cmd, workspace.cwd, true)
let res = await window.runTerminalCommand(cmd, workspace.cwd, true)
if (!res.success) {
throw new Error(`Install ${component} failed: ${res.content}`)
}
Expand All @@ -147,7 +147,7 @@ async function installComponents(config: RustupConfig): Promise<void> {
if (!hasRls) throw new Error(`${REQUIRED_COMPONENTS.join(',')} not exists in ${config.channel}`)
} catch (e) {
stopSpinner('components install failed')
workspace.showMessage(e.message)
window.showMessage(e.message)
throw e
}
stopSpinner('RLS components installed successfully')
Expand Down
27 changes: 3 additions & 24 deletions src/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
import { events, StatusBarItem, window, workspace } from 'coc.nvim'
'use strict'
import { workspace, StatusBarItem, events } from 'coc.nvim'

let statusItem: StatusBarItem = workspace.createStatusBarItem(100)
let statusItem: StatusBarItem = window.createStatusBarItem(100)
let spinnerTimer: NodeJS.Timer = null
const spinner = ['◐', '◓', '◑', '◒']
let shouldShown = true

events.on('BufEnter', async () => {
await wait(20)
let document = await workspace.document
if (document && document.filetype == 'rust') {
shouldShown = true
statusItem.show()
} else {
shouldShown = false
statusItem.hide()
}
})

function wait(ms: number): Promise<any> {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, ms)
})
}

export function startSpinner(prefix: string, postfix: string): void {
if (spinnerTimer != null) {
clearInterval(spinnerTimer)
}
let state = 0
statusItem.text = ''
if (shouldShown) statusItem.show()
statusItem.show()
spinnerTimer = setInterval(() => {
statusItem.text = prefix + ' ' + spinner[state] + ' ' + postfix
state = (state + 1) % spinner.length
Expand Down
4 changes: 2 additions & 2 deletions src/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { workspace } from 'coc.nvim'
import { window } from 'coc.nvim'

export interface Cmd {
command: string
Expand All @@ -8,7 +8,7 @@ export interface Cmd {

export function runCommand(cwd: string, command: Cmd): void {
let cmd = `${command.command} ${command.args.join(' ')}`
workspace.runTerminalCommand(cmd, cwd).catch(e => {
window.runTerminalCommand(cmd, cwd).catch(e => {
// tslint:disable-next-line: no-console
console.error(e)
})
Expand Down
Loading

0 comments on commit eaa4c93

Please sign in to comment.