Skip to content

Commit

Permalink
refactor(language_server): add own ServerCapabilities struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Nov 24, 2024
1 parent 0b4c93f commit c5a5ed7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 40 deletions.
50 changes: 10 additions & 40 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod linter;
mod server_capabilities;

use std::{fmt::Debug, path::PathBuf, str::FromStr};

Expand All @@ -13,21 +14,18 @@ use tokio::sync::{Mutex, OnceCell, RwLock, SetError};
use tower_lsp::{
jsonrpc::{Error, ErrorCode, Result},
lsp_types::{
CodeAction, CodeActionKind, CodeActionOptions, CodeActionOrCommand, CodeActionParams,
CodeActionProviderCapability, CodeActionResponse, ConfigurationItem, Diagnostic,
DiagnosticOptions, DiagnosticServerCapabilities, DidChangeConfigurationParams,
DidChangeTextDocumentParams, DidChangeWatchedFilesParams, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, DocumentDiagnosticParams, DocumentDiagnosticReport,
DocumentDiagnosticReportResult, FullDocumentDiagnosticReport, InitializeParams,
InitializeResult, InitializedParams, OneOf, RelatedFullDocumentDiagnosticReport,
ServerCapabilities, ServerInfo, TextDocumentSyncCapability, TextDocumentSyncKind, TextEdit,
Url, WorkDoneProgressOptions, WorkspaceEdit, WorkspaceFoldersServerCapabilities,
WorkspaceServerCapabilities,
CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionParams, CodeActionResponse,
ConfigurationItem, Diagnostic, DidChangeConfigurationParams, DidChangeTextDocumentParams,
DidChangeWatchedFilesParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentDiagnosticReportResult,
FullDocumentDiagnosticReport, InitializeParams, InitializeResult, InitializedParams,
RelatedFullDocumentDiagnosticReport, ServerInfo, TextEdit, Url, WorkspaceEdit,
},
Client, LanguageServer, LspService, Server,
};

use crate::linter::{DiagnosticReport, ServerLinter};
use crate::server_capabilities::ServerCapabilities;

struct Backend {
client: Client,
Expand Down Expand Up @@ -78,39 +76,11 @@ impl LanguageServer for Backend {
*self.options.lock().await = value;
}
self.init_linter_config().await;

Ok(InitializeResult {
server_info: Some(ServerInfo { name: "oxc".into(), version: None }),
offset_encoding: None,
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
workspace: Some(WorkspaceServerCapabilities {
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(OneOf::Left(true)),
}),
file_operations: None,
}),
diagnostic_provider: Some(DiagnosticServerCapabilities::Options(
DiagnosticOptions {
identifier: Some("oxc".into()),
inter_file_dependencies: false,
workspace_diagnostics: false,
work_done_progress_options: WorkDoneProgressOptions::default(),
},
)),
code_action_provider: Some(CodeActionProviderCapability::Options(
CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
resolve_provider: None,
},
)),
..ServerCapabilities::default()
},
capabilities: ServerCapabilities::from(params.capabilities).into(),
})
}

Expand Down
72 changes: 72 additions & 0 deletions crates/oxc_language_server/src/server_capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use tower_lsp::lsp_types::{
self, CodeActionKind, CodeActionOptions, CodeActionProviderCapability, DiagnosticOptions,
DiagnosticServerCapabilities, OneOf, TextDocumentSyncCapability, TextDocumentSyncKind,
WorkDoneProgressOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
};

pub struct ServerCapabilities {
diagnostic_enabled: bool,
code_action_enabled: bool,
}

impl Default for ServerCapabilities {
fn default() -> Self {
Self { diagnostic_enabled: false, code_action_enabled: false }
}
}

impl From<lsp_types::ClientCapabilities> for ServerCapabilities {
fn from(params: lsp_types::ClientCapabilities) -> Self {
if params.text_document.is_none() {
return Self::default();
}

let doc = params.text_document.unwrap();

Self {
diagnostic_enabled: doc.diagnostic.is_some(),
code_action_enabled: doc.code_action.is_some_and(|code_action| {
code_action.code_action_literal_support.is_some_and(|literal_support| {
!literal_support.code_action_kind.value_set.is_empty()
})
}),
}
}
}

impl Into<lsp_types::ServerCapabilities> for ServerCapabilities {
fn into(self) -> lsp_types::ServerCapabilities {
lsp_types::ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
workspace: Some(WorkspaceServerCapabilities {
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(OneOf::Left(true)),
}),
file_operations: None,
}),
diagnostic_provider: if self.diagnostic_enabled {
Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
identifier: Some("oxc".into()),
inter_file_dependencies: false,
workspace_diagnostics: false,
work_done_progress_options: WorkDoneProgressOptions::default(),
}))
} else {
None
},
code_action_provider: if self.code_action_enabled {
Some(CodeActionProviderCapability::Options(CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
resolve_provider: None,
}))
} else {
None
},
..lsp_types::ServerCapabilities::default()
}
}
}

0 comments on commit c5a5ed7

Please sign in to comment.