Skip to content

Commit

Permalink
Resolve linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nikeee committed Apr 2, 2024
1 parent 0ca3404 commit 77fae84
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 63 deletions.
33 changes: 18 additions & 15 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as yargs from "yargs";
import * as lsp from "vscode-languageserver/node";
import * as yargs from "yargs";
import { runServer } from "./server";

type ArgsBase = { stdio?: true; nodeIpc?: true; socket?: number; pipe?: string; }
type StdIOArgs = { stdio: true; }
type NodeIPCArgs = { nodeIpc: true; }
type SocketArgs = { socket: number; }
type PipeArgs = { pipe: string; }
type ArgsBase = { stdio?: true; nodeIpc?: true; socket?: number; pipe?: string };
type StdIOArgs = { stdio: true };
type NodeIPCArgs = { nodeIpc: true };
type SocketArgs = { socket: number };
type PipeArgs = { pipe: string };

type Args = (StdIOArgs | NodeIPCArgs | SocketArgs | PipeArgs) & ArgsBase;

// The cli is defined like this because VSCode uses this parameters on its servers
const argv = yargs
.version(require('../package.json').version)
.version(require("../package.json").version)
.options({
"stdio": {
stdio: {
type: "boolean",
description: "Use stdio",
require: false,
Expand All @@ -24,29 +24,32 @@ const argv = yargs
description: "Use node-ipc",
require: false,
},
"socket": {
socket: {
type: "number",
description: "Use socket",
require: false,
},
"pipe": {
pipe: {
type: "string",
description: "Use pipe",
require: false,
}
},
})
.help()
.argv as any as Args;
.help().argv as unknown as Args;

const setArgs = [argv.stdio, argv.socket, argv.nodeIpc, argv.pipe];

if (setArgs.every(a => !a)) {
console.error("Connection type required (stdio, node-ipc, socket, pipe). Refer to --help for more details.");
console.error(
"Connection type required (stdio, node-ipc, socket, pipe). Refer to --help for more details.",
);
process.exit(1);
}

if (setArgs.filter(a => !!a).length !== 1) {
console.error("You can only set exactly one connection type (stdio, node-ipc, socket, pipe). Refer to --help for more details.");
console.error(
"You can only set exactly one connection type (stdio, node-ipc, socket, pipe). Refer to --help for more details.",
);
process.exit(1);
}

Expand Down
83 changes: 35 additions & 48 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import type { DotLanguageServerSettings, Settings } from "./types";
import { TextDocument } from "vscode-languageserver-textdocument";
import * as lsp from "vscode-languageserver";
import { type SourceFile, createService } from "dot-language-support";
import * as rpc from "vscode-jsonrpc";
import { createService, SourceFile } from "dot-language-support";
import * as lsp from "vscode-languageserver";
import { TextDocumentSyncKind } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";

import type { DotLanguageServerSettings, Settings } from "./types";

const defaultSettings: DotLanguageServerSettings = { maxNumberOfProblems: 100 };

export function runServer(connection: lsp.Connection) {
if (!connection)
throw "connection is missing";
if (!connection) throw "connection is missing";

const languageService = createService();

// Create a simple text document manager.
// The text document manager supports full document sync only
let documents = new lsp.TextDocuments(TextDocument);
const documents = new lsp.TextDocuments(TextDocument);
const astOfFile = new Map<string, SourceFile>();

// Make the documents listen for changes on the connection
documents.listen(connection);

let shouldSendDiagnosticRelatedInformation: boolean = false;
let shouldSendDiagnosticRelatedInformation = false;

// After the server has started the client sends an initialize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilities.
connection.onInitialize((_params): lsp.InitializeResult => {
let a = _params.capabilities && _params.capabilities.textDocument && _params.capabilities.textDocument.publishDiagnostics && _params.capabilities.textDocument.publishDiagnostics.relatedInformation;
const a = _params.capabilities?.textDocument?.publishDiagnostics?.relatedInformation;
shouldSendDiagnosticRelatedInformation = !!a;

return {
Expand All @@ -42,45 +42,40 @@ export function runServer(connection: lsp.Connection) {
renameProvider: true,
codeActionProvider: true,
executeCommandProvider: {
commands: languageService.getAvailableCommands(),
commands: languageService.getAvailableCommands(),
},
colorProvider: true,
// documentFormattingProvider: true,
}
}
},
};
});

function rebuildAll() {
for (const uri of astOfFile.keys())
updateAst(uri);
for (const uri of astOfFile.keys()) updateAst(uri);
}

function updateAst(uri: string, doc?: TextDocument): SourceFile | undefined {
if (doc === undefined)
doc = documents.get(uri);

if (doc) {
const ast = languageService.parseDocument(doc)
astOfFile.set(uri, ast);
return ast;
const d = doc === undefined ? documents.get(uri) : doc;
if (!d) {
return undefined;
}
return undefined;

const ast = languageService.parseDocument(d);
astOfFile.set(uri, ast);
return ast;
}

function ensureAst(uri: string, doc?: TextDocument): SourceFile | undefined {
let ast = astOfFile.get(uri);
if (ast === undefined)
ast = updateAst(uri, doc);
if (ast === undefined) ast = updateAst(uri, doc);
return ast;
}

connection.onHover(req => {
const uri = req.textDocument.uri;
const doc = documents.get(uri);
const ast = ensureAst(uri, doc);
return doc && ast
? languageService.hover(doc, ast, req.position)
: invalidRequest();
return doc && ast ? languageService.hover(doc, ast, req.position) : invalidRequest();
});

connection.onReferences(req => {
Expand All @@ -105,12 +100,9 @@ export function runServer(connection: lsp.Connection) {
const uri = req.textDocument.uri;
const doc = documents.get(uri);
const ast = ensureAst(uri, doc);
return doc && ast
? languageService.getDocumentColors(doc, ast)
: invalidRequest();
return doc && ast ? languageService.getDocumentColors(doc, ast) : invalidRequest();
});


connection.onColorPresentation(req => {
const uri = req.textDocument.uri;
const doc = documents.get(uri);
Expand All @@ -120,7 +112,6 @@ export function runServer(connection: lsp.Connection) {
: invalidRequest();
});


/**
* Event that gathers possible code actions
*/
Expand All @@ -134,12 +125,10 @@ export function runServer(connection: lsp.Connection) {
// Put the URI of the current document to the end
// We need the uri to get the AST later
if (r) {
r.forEach(command => {
if (command.arguments)
command.arguments.push(uri);
else
command.arguments = [uri];
});
for (const command of r) {
if (command.arguments) command.arguments.push(uri);
else command.arguments = [uri];
}
}

return r;
Expand All @@ -152,8 +141,7 @@ export function runServer(connection: lsp.Connection) {
*/
connection.onExecuteCommand(req => {
const args = req.arguments;
if (!args || args.length < 1)
return;
if (!args || args.length < 1) return;

// Remove the URI and retrieve AST
const uri = args.pop();
Expand All @@ -163,8 +151,7 @@ export function runServer(connection: lsp.Connection) {
req.arguments = args.length === 0 ? undefined : args;
const edit = languageService.executeCommand(doc, ast, req);

if (edit)
connection.workspace.applyEdit(edit);
if (edit) connection.workspace.applyEdit(edit);
}
});

Expand Down Expand Up @@ -198,8 +185,7 @@ export function runServer(connection: lsp.Connection) {
// The settings have changed. Is send on server activation as well.
connection.onDidChangeConfiguration(change => {
const newSettings = (change.settings as Settings).dotLanguageServer;
if (newSettings)
currentSettings = newSettings;
if (newSettings) currentSettings = newSettings;

rebuildAll();
validateAll();
Expand All @@ -210,8 +196,7 @@ export function runServer(connection: lsp.Connection) {
const doc = documents.get(uri);
if (doc) {
const ast = ensureAst(uri, doc);
if (ast)
validateDocument(doc, ast);
if (ast) validateDocument(doc, ast);
}
}
}
Expand All @@ -233,13 +218,15 @@ export function runServer(connection: lsp.Connection) {
const doc = documents.get(uri);
const ast = ensureAst(uri, doc);
return doc && ast
? languageService.getCompletions(doc, ast, req.position) as any
? // biome-ignore lint/suspicious/noExplicitAny: :shrug:
(languageService.getCompletions(doc, ast, req.position) as any)
: invalidRequest();
});

documents.onDidOpen(params => updateAst(params.document.uri, params.document));

const invalidRequest = () => new rpc.ResponseError<void>(rpc.ErrorCodes.InvalidRequest, "Invalid request");
const invalidRequest = () =>
new rpc.ResponseError<void>(rpc.ErrorCodes.InvalidRequest, "Invalid request");

connection.listen();
}

0 comments on commit 77fae84

Please sign in to comment.