-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add expermiental support for sumneko/lua-language-server
- Loading branch information
Showing
6 changed files
with
266 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"coc.preferences.formatOnSaveFiletypes": ["javascript", "typescript", "json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,68 @@ | ||
import {commands, ExtensionContext, LanguageClient, ServerOptions, workspace, services, LanguageClientOptions} from 'coc.nvim' | ||
import {installLuaLsp, luaLspBin, commandExists} from './utils/tools' | ||
import {version, updateLuaLsp} from './commands' | ||
import {setStoragePath} from './utils/config' | ||
import { | ||
commands, | ||
ExtensionContext, | ||
LanguageClient, | ||
ServerOptions, | ||
workspace, | ||
services, | ||
LanguageClientOptions, | ||
} from "coc.nvim" | ||
import { installLuaLsp, luaLspBin, commandExists } from "./utils/tools" | ||
import { version, updateLuaLsp } from "./commands" | ||
import { setStoragePath } from "./utils/config" | ||
|
||
interface LuaConfig { | ||
enable: boolean | ||
commandPath: string | ||
} | ||
|
||
export async function activate(context: ExtensionContext): Promise<void> { | ||
|
||
setStoragePath(context.storagePath) | ||
|
||
const config = workspace.getConfiguration().get('lua', {}) as LuaConfig | ||
const config = workspace.getConfiguration().get("lua", {}) as LuaConfig | ||
if (config.enable === false) { | ||
return | ||
} | ||
|
||
const command = config.commandPath || await luaLspBin() | ||
if (!await commandExists(command)) { | ||
await installLuaLsp() | ||
const [command, args] = config.commandPath ? [config.commandPath, []] : await luaLspBin() | ||
|
||
if (!(await commandExists(command))) { | ||
const useSumnekoLs = workspace.getConfiguration().get("lua.useSumnekoLs", false) | ||
|
||
const name = useSumnekoLs ? "sumneko/lua-language-server" : "Alloyed/lua-lsp" | ||
await showInstallStatus(name, async () => { | ||
await installLuaLsp() | ||
}) | ||
} | ||
|
||
const serverOptions: ServerOptions = {command} | ||
const serverOptions: ServerOptions = { command, args } | ||
|
||
const clientOptions: LanguageClientOptions = { | ||
documentSelector: ['lua'] | ||
documentSelector: ["lua"], | ||
} | ||
|
||
const client = new LanguageClient('lua', 'lua-lsp', serverOptions, clientOptions) | ||
const client = new LanguageClient("lua", "lua-lsp", serverOptions, clientOptions) | ||
|
||
context.subscriptions.push( | ||
services.registLanguageClient(client), | ||
commands.registerCommand("lua.version", () => version()), | ||
commands.registerCommand("lua.update.lua-lsp", () => updateLuaLsp(client)), | ||
commands.registerCommand("lua.update.lua-lsp", () => updateLuaLsp(client)) | ||
) | ||
} | ||
|
||
async function showInstallStatus(name: string, fn: () => Promise<void>) { | ||
const statusItem = workspace.createStatusBarItem(90, { progress: true }) | ||
|
||
statusItem.text = `Installing '${name}'` | ||
statusItem.show() | ||
|
||
try { | ||
await fn() | ||
|
||
workspace.showMessage(`Installed '${name}'`) | ||
} catch (err) { | ||
workspace.showMessage(`Failed to install '${name}'`, "error") | ||
} | ||
|
||
statusItem.hide() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// https://githb.com/neovim/nvim-lspconfi/blob/e38ff05afc3ad5d4fa8b24b4b0619429125582de/la/nvim_lsp/sumneko_lua.lua | ||
|
||
import crypto from "crypto" | ||
import fs from "fs" | ||
import fsp from "fs/promises" | ||
import https from "https" | ||
import os from "os" | ||
import path from "path" | ||
import unzipper from "unzipper" | ||
import { exec } from "child_process" | ||
|
||
const ninjaVersion = "v1.9.0" | ||
const osPlatform = os.platform() | ||
const tmpBaseDir = os.tmpdir() | ||
|
||
const { join } = path | ||
|
||
export async function install(dir: string): Promise<void> { | ||
const { path: tmpDir, dispose } = await mkTmpDir("coc-lua") | ||
const { ninjaZip, buildFile } = osEnv() | ||
|
||
const ninjaUrl = `https://github.com/ninja-build/ninja/releases/download/${ninjaVersion}/${ninjaZip}` | ||
const llsUrl = "https://github.com/sumneko/lua-language-server.git" | ||
|
||
const binDir = join(tmpDir, "bin") | ||
const llsDir = join(tmpDir, "lua-ls") | ||
|
||
const env = { ...process.env, PATH: `${process.env.PATH}:${binDir}` } | ||
|
||
// Install ninja | ||
await downloadZip(ninjaUrl, binDir) | ||
await fsp.chmod(join(binDir, "ninja"), 0o755) | ||
|
||
// clone | ||
await sh(`git clone "${llsUrl}" "${llsDir}"`) | ||
await sh("git submodule update --init --recursive", { cwd: llsDir }) | ||
|
||
// build | ||
await sh(`ninja -f ${join("ninja", buildFile)}`, { cwd: join(llsDir, "3rd", "luamake"), env }) | ||
await sh(`${join(llsDir, "3rd", "luamake", "luamake")} rebuild`, { cwd: llsDir, env }) | ||
|
||
// copy files | ||
for (const p of ["bin", "libs", "locale", "script", "main.lua", "platform.lua"]) { | ||
await copy(join(llsDir, p), join(dir, p)) | ||
} | ||
|
||
await dispose() | ||
} | ||
|
||
async function downloadZip(sourceUrl: string, targetPath: string) { | ||
const dir = await mkTmpDir(sourceUrl) | ||
|
||
const zipTmpPath = join(dir.path, "tmp.zip") | ||
|
||
await download(sourceUrl, zipTmpPath) | ||
await extractZip(zipTmpPath, targetPath) | ||
await dir.dispose() | ||
} | ||
|
||
async function mkTmpDir(key: string): Promise<{ path: string; dispose: () => Promise<void> }> { | ||
const hash = crypto.createHash("md5").update(key).digest("hex") | ||
const dir = join(tmpBaseDir, hash) | ||
|
||
await fsp.mkdir(dir, { recursive: true }) | ||
|
||
return { path: dir, dispose: async () => fsp.rmdir(dir, { recursive: true }) } | ||
} | ||
|
||
async function download(sourceUrl: string, targetPath: string) { | ||
const file = fs.createWriteStream(targetPath) | ||
|
||
return new Promise((resolve, reject) => { | ||
const get = (url: string) => | ||
https.get(url, (res) => { | ||
const { statusCode } = res | ||
|
||
if (statusCode === 301 || statusCode === 302) { | ||
return get(res.headers.location) | ||
} | ||
|
||
res | ||
.on("data", (data) => file.write(data)) | ||
.on("end", () => (file.end(), resolve())) | ||
.on("error", (err) => reject(err)) | ||
}) | ||
|
||
return get(sourceUrl) | ||
}) | ||
} | ||
|
||
export function osEnv(): { ninjaZip: string; buildFile: string; bin: string } { | ||
switch (osPlatform) { | ||
case "darwin": | ||
return { | ||
ninjaZip: "ninja-mac.zip", | ||
buildFile: "macos.ninja", | ||
bin: join("bin", "macOS", "lua-language-server"), | ||
} | ||
case "linux": | ||
return { | ||
ninjaZip: "ninja-linux.zip", | ||
buildFile: "linux.ninja", | ||
bin: join("bin", "macOS", "lua-language-server"), | ||
} | ||
case "win32": | ||
return { | ||
ninjaZip: "ninja-win.zip", | ||
buildFile: "msvc.ninja", | ||
bin: join("bin", "macOS", "lua-language-server.exe"), | ||
} | ||
} | ||
return { ninjaZip: "", buildFile: "", bin: "" } | ||
} | ||
|
||
async function extractZip(zipPath: string, outputPath: string) { | ||
return new Promise((resolve) => { | ||
const extract = unzipper.Extract({ path: outputPath }) | ||
extract.on("close", resolve) | ||
|
||
fs.createReadStream(zipPath).pipe(extract) | ||
}) | ||
} | ||
|
||
async function sh(cmd: string, options?: { cwd?: string; env?: { [key: string]: string } }): Promise<[string, string]> { | ||
return new Promise((resolve, reject) => { | ||
exec(cmd, options || {}, (err: Error, stdout: string, stderr: string) => { | ||
if (err !== null) { | ||
return reject(err) | ||
} | ||
|
||
resolve([stdout as string, stderr as string]) | ||
}) | ||
}) | ||
} | ||
|
||
async function copy(src: string, dest: string) { | ||
try { | ||
const isDirectory = (await fsp.stat(src)).isDirectory() | ||
if (isDirectory) { | ||
await fsp.mkdir(dest, { recursive: true }) | ||
|
||
for (const file of await fsp.readdir(src)) { | ||
await copy(join(src, file), join(dest, file)) | ||
} | ||
} else { | ||
await fsp.copyFile(src, dest) | ||
} | ||
} catch (err) { | ||
// empty | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,53 @@ | ||
import path from 'path' | ||
import fs from 'fs' | ||
import {workspace} from 'coc.nvim' | ||
import which from 'which' | ||
import {configDir} from './config' | ||
import path from "path" | ||
import fs from "fs" | ||
import { workspace } from "coc.nvim" | ||
import which from "which" | ||
import { configDir } from "./config" | ||
import { osEnv, install } from "./installer" | ||
|
||
const lspDir = "sumneko_lua" | ||
|
||
export async function installLuaLsp(force = false): Promise<void> { | ||
if (!force && await luaLspExists()) { | ||
if (!force && (await luaLspExists())) { | ||
return | ||
} | ||
const baseDir = await configDir('tools') | ||
|
||
const useSumnekoLs = workspace.getConfiguration().get("lua.useSumnekoLs", false) | ||
if (useSumnekoLs) { | ||
return install(await configDir("tools", lspDir)) | ||
} | ||
|
||
const baseDir = await configDir("tools") | ||
let installCmd = `luarocks install --tree ${baseDir} --server=http://luarocks.org/dev lua-lsp` | ||
|
||
const luaVersion = workspace.getConfiguration().get('lua', {})['version'] | ||
if(luaVersion) { | ||
const luaVersion = workspace.getConfiguration().get("lua", {})["version"] | ||
if (luaVersion) { | ||
installCmd += ` --lua-version=${luaVersion}` | ||
} | ||
|
||
await workspace.runTerminalCommand(installCmd) | ||
} | ||
|
||
export async function luaLspBin(): Promise<string> { | ||
let executable = 'lua-lsp' | ||
if (process.platform === 'win32') { | ||
// binary installed by luarocks under Windows has extension '.bat' | ||
executable += '.bat' | ||
export async function luaLspBin(): Promise<[string, string[]]> { | ||
const baseDir = await configDir("tools") | ||
|
||
const useSumnekoLs = workspace.getConfiguration().get("lua.useSumnekoLs", false) | ||
if (useSumnekoLs) { | ||
const { bin } = osEnv() | ||
return [path.join(baseDir, lspDir, bin), ["-E", path.join(baseDir, lspDir, "main.lua")]] | ||
} | ||
return path.join(await configDir('tools', 'bin'), executable) | ||
|
||
// binary installed by luarocks under Windows has extension '.bat' | ||
const bin = process.platform === "win32" ? "lua-lsp.bat" : "lua-lsp" | ||
|
||
return [path.join(baseDir, "bin", bin), []] | ||
} | ||
|
||
export async function luaLspExists(): Promise<boolean> { | ||
const bin = await luaLspBin() | ||
return new Promise(resolve => fs.open(bin, 'r', (err) => resolve(err === null))) | ||
const [bin] = await luaLspBin() | ||
return new Promise((resolve) => fs.open(bin, "r", (err) => resolve(err === null))) | ||
} | ||
|
||
export async function commandExists(command: string): Promise<boolean> { | ||
return new Promise(resolve => which(command, (err) => resolve(err == null))) | ||
return new Promise((resolve) => which(command, (err) => resolve(err == null))) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters