This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add code completion using gopls
- Loading branch information
Showing
4 changed files
with
214 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
logs/ | ||
node_modules/ | ||
*.log |
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,116 @@ | ||
let client = null; | ||
|
||
const debugAddress = "localhost:6060"; | ||
|
||
// TODO: make this Disposable class | ||
async function start() { | ||
stop(); | ||
console.log("initializing gopls..."); | ||
|
||
// Try to get gopls path from config | ||
let goplsPath = nova.config.get("go.gopls-path"); | ||
|
||
// Try to get gopls path from PATH unless custom configuration provided | ||
if (!goplsPath) { | ||
goplsPath = await new Promise((resolve, reject) => { | ||
const which = new Process("/usr/bin/env", { | ||
args: ["which", "gopls"], | ||
}); | ||
nova.subscriptions.add( | ||
which.onDidExit((status) => { | ||
if (status !== 0) reject(false); | ||
}) | ||
); | ||
nova.subscriptions.add( | ||
which.onStdout((text) => { | ||
resolve(text.trim()); | ||
}) | ||
); | ||
which.start(); | ||
}); | ||
} | ||
|
||
// If we can't find gopls, use the default path | ||
if (!goplsPath) { | ||
let gobin = nova.environment.GOBIN; | ||
if (!gobin) { | ||
const gopath = await new Promise((resolve, reject) => { | ||
const go = new Process("/usr/bin/env", { | ||
args: ["go", "env", "GOPATH"], | ||
}); | ||
nova.subscriptions.add( | ||
go.onDidExit((status) => { | ||
if (status !== 0) { | ||
reject(`go env GOPATH exited with status code: ${status}`); | ||
} | ||
}) | ||
); | ||
nova.subscriptions.add( | ||
go.onStdout((text) => { | ||
resolve(text.trim()); | ||
}) | ||
); | ||
go.start(); | ||
}); | ||
gobin = nova.path.join(gopath, "bin"); | ||
} | ||
goplsPath = nova.path.join(gobin, "gopls"); | ||
} | ||
|
||
console.log(`gopls path: ${goplsPath}`); | ||
|
||
// Create the client | ||
let serverOptions = { | ||
type: "stdio", | ||
path: goplsPath, | ||
args: [], | ||
}; | ||
if (nova.inDevMode()) { | ||
console.log(`debug information will be available on ${debugAddress}`); | ||
|
||
const logDir = nova.path.join(nova.workspace.path, "logs"); | ||
await new Promise((resolve, reject) => { | ||
const mkdir = new Process("/usr/bin/env", { | ||
args: ["mkdir", "-p", logDir], | ||
}); | ||
nova.subscriptions.add( | ||
mkdir.onDidExit((status) => { | ||
if (status !== 0) { | ||
reject(status); | ||
} | ||
resolve(status); | ||
}) | ||
); | ||
|
||
mkdir.start(); | ||
}); | ||
const logFile = nova.path.join(logDir, "gopls.log"); | ||
console.log(`gopls log file: ${logFile}`); | ||
|
||
serverOptions.args.push("-debug", debugAddress); | ||
serverOptions.args.push("-logfile", logFile); | ||
serverOptions.args.push("-rpc.trace"); | ||
serverOptions.args.push("-v"); | ||
serverOptions.args.push("-vv"); | ||
} | ||
|
||
client = new LanguageClient("gopls", "Go Language Server", serverOptions, { | ||
syntaxes: ["go", "gomod"], | ||
}); | ||
nova.subscriptions.add(client); | ||
|
||
console.log("starting language client..."); | ||
client.start(); | ||
} | ||
|
||
function stop() { | ||
nova.subscriptions.dispose(); | ||
|
||
if (client) { | ||
client.stop(); | ||
client = null; | ||
} | ||
} | ||
|
||
exports.start = start; | ||
exports.stop = stop; |
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,69 @@ | ||
const gopls = require("gopls.js"); | ||
|
||
exports.activate = function () { | ||
console.log("activating"); | ||
|
||
(async () => { | ||
await start(); | ||
if (nova.config.get("go.enable-gopls")) { | ||
await gopls.start(); | ||
} | ||
console.log("activation completed"); | ||
})().catch((err) => { | ||
if (!nova.inDevMode()) return; | ||
console.error("error activating:", err); | ||
}); | ||
}; | ||
|
||
exports.deactivate = function () { | ||
console.log("deactivating"); | ||
console.log("deactivation completed"); | ||
}; | ||
|
||
async function start() { | ||
// Check if the go command is available | ||
const isGoAvailable = await new Promise((resolve, reject) => { | ||
const which = new Process("/usr/bin/env", { | ||
args: ["which", "go"], | ||
}); | ||
nova.subscriptions.add( | ||
which.onDidExit((status) => { | ||
if (status !== 0) { | ||
reject(false); | ||
return; | ||
} | ||
resolve(true); | ||
}) | ||
); | ||
which.start(); | ||
}); | ||
if (!isGoAvailable) { | ||
throw new Error("cannot find go executable"); | ||
} | ||
|
||
// Check if the go version is correct | ||
const goVersion = await new Promise((resolve, reject) => { | ||
const go = new Process("/usr/bin/env", { | ||
args: ["go", "version"], | ||
}); | ||
nova.subscriptions.add( | ||
go.onDidExit((status) => { | ||
if (status !== 0) { | ||
reject(`go version exited with status code: ${status}`); | ||
} | ||
}) | ||
); | ||
nova.subscriptions.add( | ||
go.onStdout((text) => { | ||
resolve(text.trim()); | ||
}) | ||
); | ||
go.start(); | ||
}); | ||
if (!versionRegexp.exec(goVersion)) { | ||
throw new Error(`${goVersion} is not compatible with this extension`); | ||
} | ||
console.log(goVersion); | ||
} | ||
|
||
const versionRegexp = /go version go1\.[\d]+(?:\.[\d])?(?:[\w\d]+)? darwin\/[\w\d]+/; |
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