Skip to content

Commit

Permalink
refactor(ctx): ctx.resolveBin
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Feb 7, 2020
1 parent f57d523 commit 7d9a11b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
40 changes: 1 addition & 39 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
import { Executable, LanguageClient, LanguageClientOptions, ServerOptions, Uri, workspace } from 'coc.nvim';
import executable from 'executable';
import { existsSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import which from 'which';
import { Config } from './config';

function expandPathResolving(path: string) {
if (path.startsWith('~/')) {
return path.replace('~', homedir());
}
return path;
}

export function resolveBin(config: Config, serverRoot: string): string | undefined {
// 1. from config
// 2. in PATH
// 3. in coc-server-root
let bin = expandPathResolving(config.raLspServerPath);
if (bin === 'ra_lsp_server') {
bin = which.sync(bin, { nothrow: true }) || join(serverRoot, bin);
}

if (!existsSync(bin)) {
return;
}

if (!executable.sync(bin)) {
workspace.showMessage(`${bin} is not executable`, 'error');
return;
}

return bin;
}

export function createClient(config: Config, serverRoot: string): LanguageClient | undefined {
const bin = resolveBin(config, serverRoot);
if (!bin) {
return;
}

export function createClient(config: Config, bin: string): LanguageClient | undefined {
let folder = '.';
if (workspace.workspaceFolder?.uri.length > 0) {
folder = Uri.parse(workspace.workspaceFolder.uri).fsPath;
Expand Down
6 changes: 2 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { workspace, commands } from 'coc.nvim';

const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;

export interface CargoWatchOptions {
enable: boolean;
arguments: string[];
Expand All @@ -19,7 +17,7 @@ export class Config {
public highlightingOn = true;
public rainbowHighlightingOn = false;
public enableEnhancedTyping = true;
public raLspServerPath = RA_LSP_DEBUG || 'ra_lsp_server';
public raLspServerPath = '';
public lruCapacity: null | number = null;
public displayInlayHints = true;
public excludeGlobs: string[] = [];
Expand Down Expand Up @@ -78,7 +76,7 @@ export class Config {
}

if (config.has('raLspServerPath')) {
this.raLspServerPath = RA_LSP_DEBUG || (config.get('raLspServerPath') as string);
this.raLspServerPath = config.get<string>('raLspServerPath', '');
}

if (config.has('cargo-watch.enable')) {
Expand Down
35 changes: 33 additions & 2 deletions src/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { commands, ExtensionContext, LanguageClient, services } from 'coc.nvim';
import { commands, ExtensionContext, LanguageClient, services, workspace } from 'coc.nvim';
import executable from 'executable';
import { existsSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import { createClient } from './client';
import { Config } from './config';

Expand All @@ -23,13 +27,18 @@ export class Ctx {
}

async restartServer() {
const bin = this.resolveBin();
if (!bin) {
return;
}

const old = this.client;
if (old) {
await old.stop();
}

this.client = null;
const client = createClient(this.config, this.extCtx.storagePath);
const client = createClient(this.config, bin);
if (!client) {
return;
}
Expand All @@ -55,4 +64,26 @@ export class Ctx {
onDidRestart(hook: (client: LanguageClient) => void) {
this.onDidRestartHooks.push(hook);
}

resolveBin(): string | undefined {
// 1. bundled in coc-server-root
// 2. from config
let bin = join(this.extCtx.storagePath, process.platform === 'win32' ? 'ra_lsp_server.exe' : 'ra_lsp_server');
if (!existsSync(bin) && this.config.raLspServerPath.length > 0) {
bin = this.config.raLspServerPath;
if (bin.startsWith('~/')) {
bin = bin.replace('~', homedir());
}
if (!existsSync(bin)) {
return;
}
}

if (!executable.sync(bin)) {
workspace.showMessage(`${bin} is not executable`, 'error');
return;
}

return bin;
}
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ExtensionContext, workspace } from 'coc.nvim';
import { existsSync, mkdirSync } from 'fs';
import { resolveBin } from './client';
import * as cmds from './cmds';
import { Ctx } from './ctx';
import { downloadServer } from './downloader';
Expand All @@ -14,7 +13,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
mkdirSync(serverRoot);
}

const bin = resolveBin(ctx.config, serverRoot);
const bin = ctx.resolveBin();
if (!bin) {
let msg = 'ra_lsp_server is not found, download from GitHub release?';
const ret = await workspace.showQuickpick(['Download', 'Cancel'], msg);
Expand Down

0 comments on commit 7d9a11b

Please sign in to comment.