Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move server init params to rust #35

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 96 additions & 4 deletions kls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ use lsp_types::{
DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument, DidDeleteFiles,
DidOpenTextDocument, DidSaveTextDocument, Initialized, Notification,
},
request::{Formatting, GotoDefinition, Request},
request::{Formatting, GotoDefinition, Initialize, Request, SemanticTokensFullRequest},
DeleteFilesParams, Diagnostic, DiagnosticSeverity, DiagnosticTag, DidChangeTextDocumentParams,
DidChangeWatchedFilesParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams,
DocumentFormattingParams, FileChangeType, FileDelete, FileEvent, GotoDefinitionParams,
GotoDefinitionResponse, InitializeParams, LocationLink, Position, PublishDiagnosticsParams,
TextDocumentItem, TextEdit, Url, VersionedTextDocumentIdentifier,
DocumentFormattingParams, FileChangeType, FileDelete, FileEvent, FileOperationFilter,
FileOperationPattern, GotoDefinitionParams, GotoDefinitionResponse, InitializeParams,
InitializeResult, LocationLink, Position, PositionEncodingKind, PublishDiagnosticsParams,
SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens,
SemanticTokensFullOptions, SemanticTokensLegend, SemanticTokensParams, SemanticTokensResult,
TextDocumentItem, TextDocumentSyncKind, TextEdit, Url, VersionedTextDocumentIdentifier,
};
use serde::Deserialize;
use serde_wasm_bindgen::{from_value, to_value};
Expand Down Expand Up @@ -324,6 +327,95 @@ impl KanataLanguageServer {
// self_
}

#[allow(unused_variables)]
#[wasm_bindgen(js_class = KanataLanguageServer, js_name = initialize)]
pub fn initialize(&mut self, params: JsValue) -> JsValue {
type Params = <Initialize as Request>::Params;
type Result = <Initialize as Request>::Result;
let params = from_value::<Params>(params).expect("deserializes");
to_value::<Result>(&self.initialize_impl(&params)).expect("no conversion error")
}

fn initialize_impl(&mut self, _params: &InitializeParams) -> InitializeResult {
let sem_tokens_legend = SemanticTokensLegend {
token_types: vec![
SemanticTokenType::NAMESPACE,
SemanticTokenType::TYPE,
SemanticTokenType::CLASS,
SemanticTokenType::ENUM,
SemanticTokenType::INTERFACE,
SemanticTokenType::STRUCT,
SemanticTokenType::TYPE_PARAMETER,
SemanticTokenType::PARAMETER,
SemanticTokenType::VARIABLE,
SemanticTokenType::PROPERTY,
SemanticTokenType::ENUM_MEMBER,
SemanticTokenType::EVENT,
SemanticTokenType::FUNCTION,
SemanticTokenType::METHOD,
SemanticTokenType::MACRO,
SemanticTokenType::KEYWORD,
SemanticTokenType::MODIFIER,
SemanticTokenType::COMMENT,
SemanticTokenType::STRING,
SemanticTokenType::NUMBER,
SemanticTokenType::REGEXP,
SemanticTokenType::OPERATOR,
],
token_modifiers: vec![
SemanticTokenModifier::DECLARATION,
SemanticTokenModifier::DEFINITION,
SemanticTokenModifier::READONLY,
SemanticTokenModifier::STATIC,
SemanticTokenModifier::DEPRECATED,
SemanticTokenModifier::ABSTRACT,
SemanticTokenModifier::ASYNC,
SemanticTokenModifier::MODIFICATION,
SemanticTokenModifier::DOCUMENTATION,
SemanticTokenModifier::DEFAULT_LIBRARY,
],
};

InitializeResult {
capabilities: lsp_types::ServerCapabilities {
// UTF-8 is not supported in vscode-languageserver/node. See:
// https://github.com/microsoft/vscode-languageserver-node/issues/1224
position_encoding: Some(PositionEncodingKind::UTF16),
// textDocumentSync: {
// openClose: true,
// save: { includeText: false },
// change: TextDocumentSyncKind.Full,
// },
text_document_sync: Some(lsp_types::TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
definition_provider: Some(lsp_types::OneOf::Left(true)),
document_formatting_provider: Some(lsp_types::OneOf::Left(true)),
workspace: Some(lsp_types::WorkspaceServerCapabilities {
workspace_folders: Some(lsp_types::WorkspaceFoldersServerCapabilities {
supported: Some(false),
change_notifications: None,
}),
file_operations: Some(lsp_types::WorkspaceFileOperationsServerCapabilities {
did_delete: Some(lsp_types::FileOperationRegistrationOptions {
filters: vec![FileOperationFilter {
scheme: None,
pattern: FileOperationPattern {
glob: "**".to_string(),
matches: None,
options: None,
},
}],
}),
..Default::default()
}),
}),
..Default::default()
},
server_info: None,
}
}

/// Catch-all handler for notifications sent by the LSP client.
///
/// This function receives a notification's `method` and `params` and dispatches to the
Expand Down
46 changes: 14 additions & 32 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,31 @@ import {
createConnection,
ProposedFeatures,
PublishDiagnosticsParams,
TextDocumentSyncKind,
InitializeParams,
PositionEncodingKind,
} from "vscode-languageserver/node";
import { KanataLanguageServer } from "../../out/kls";

// Create LSP connection
const connection = createConnection(ProposedFeatures.all);

const sendDiagnosticsCallback = (params: PublishDiagnosticsParams) =>
connection.sendDiagnostics(params);

connection.onInitialize((params: InitializeParams) => {
const kls = new KanataLanguageServer(params, sendDiagnosticsCallback);

const kls = new KanataLanguageServer(
params,
(params: PublishDiagnosticsParams) => connection.sendDiagnostics(params),
);
connection.onNotification((...args) => kls.onNotification(...args));
connection.onDocumentFormatting((...args) =>
kls.onDocumentFormatting(...args),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
kls.onDocumentFormatting(args[0]),
);
connection.onDefinition((...args) => kls.onDefinition(...args));

return {
capabilities: {
textDocumentSync: {
openClose: true,
save: { includeText: false },
change: TextDocumentSyncKind.Full,
},
// UTF-8 is not supported in vscode-languageserver/node. See:
// https://github.com/microsoft/vscode-languageserver-node/issues/1224
positionEncoding: PositionEncodingKind.UTF16,
documentFormattingProvider: true,
workspace: {
workspaceFolders: { supported: false },
fileOperations: {
didDelete: {
filters: [{ pattern: { /* matches: 'folder', */ glob: "**" } }],
},
},
},
definitionProvider: true,
},
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
connection.onDefinition((...args) => kls.onDefinition(args[0]));
connection.languages.semanticTokens.on((...args) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
kls.onSemanticTokens(args[0]),
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
return kls.initialize(params);
});

connection.listen();
Loading