Skip to content

Commit

Permalink
fix: Don't send empty response to Helix client
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis committed Oct 15, 2024
1 parent 228f054 commit 96c6a7e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 53 deletions.
24 changes: 21 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::path::PathBuf;

use asm_lsp::types::LspClient;

use asm_lsp::handle::{
handle_completion_request, handle_diagnostics, handle_did_change_text_document_notification,
handle_did_close_text_document_notification, handle_did_open_text_document_notification,
Expand Down Expand Up @@ -100,12 +102,18 @@ pub fn main() -> Result<()> {
let server_capabilities = serde_json::to_value(capabilities).unwrap();
let initialization_params = connection.initialize(server_capabilities)?;

let mut names_to_info = NameToInfoMaps::default();
let params: InitializeParams = serde_json::from_value(initialization_params.clone()).unwrap();
info!("Client initialization params: {:?}", params);
let config = get_config(&params);
let mut config = get_config(&params);
info!("Server Configuration: {:?}", config);
if let Some(ref client_info) = params.client_info {
if client_info.name.eq("helix") {
info!("Helix LSP client detected");
config.client = Some(LspClient::Helix);
}
}

let mut names_to_info = NameToInfoMaps::default();
// create a map of &Instruction_name -> &Instruction - Use that in user queries
// The Instruction(s) themselves are stored in a vector and we only keep references to the
// former map
Expand Down Expand Up @@ -447,7 +455,14 @@ fn main_loop(
start.elapsed().as_millis()
);
} else if let Ok((id, params)) = cast_req::<GotoDefinition>(req.clone()) {
handle_goto_def_request(connection, id, &params, &text_store, &mut tree_store)?;
handle_goto_def_request(
connection,
id,
&params,
config,
&text_store,
&mut tree_store,
)?;
info!(
"Goto definition request serviced in {}ms",
start.elapsed().as_millis()
Expand All @@ -457,6 +472,7 @@ fn main_loop(
connection,
id,
&params,
config,
&text_store,
&mut tree_store,
)?;
Expand All @@ -469,6 +485,7 @@ fn main_loop(
connection,
id,
&params,
config,
&text_store,
&mut tree_store,
&names_to_info.instructions,
Expand All @@ -482,6 +499,7 @@ fn main_loop(
connection,
id,
&params,
config,
&text_store,
&mut tree_store,
)?;
Expand Down
59 changes: 13 additions & 46 deletions src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use tree_sitter::Parser;
use crate::{
apply_compile_cmd, get_comp_resp, get_default_compile_cmd, get_document_symbols,
get_goto_def_resp, get_hover_resp, get_ref_resp, get_sig_help_resp, get_word_from_pos_params,
text_doc_change_to_ts_edit, Config, NameToInfoMaps, NameToInstructionMap, TreeEntry, TreeStore,
send_empty_resp, text_doc_change_to_ts_edit, Config, NameToInfoMaps, NameToInstructionMap,
TreeEntry, TreeStore,
};

/// Handles hover requests
Expand All @@ -43,20 +44,12 @@ pub fn handle_hover_request(
names_to_info: &NameToInfoMaps,
include_dirs: &HashMap<SourceFile, Vec<PathBuf>>,
) -> Result<()> {
let empty_resp = Response {
id: id.clone(),
result: None,
error: None,
};

let word = if let Some(doc) =
text_store.get_document(&params.text_document_position_params.text_document.uri)
{
get_word_from_pos_params(doc, &params.text_document_position_params)
} else {
return Ok(connection
.sender
.send(Message::Response(empty_resp.clone()))?);
return send_empty_resp(connection, id, config);
};

if let Some(hover_resp) = get_hover_resp(
Expand All @@ -79,7 +72,7 @@ pub fn handle_hover_request(
return Ok(connection.sender.send(Message::Response(result))?);
}

Ok(connection.sender.send(Message::Response(empty_resp))?)
send_empty_resp(connection, id, config)
}

/// Handles completion requests
Expand Down Expand Up @@ -126,13 +119,7 @@ pub fn handle_completion_request(
}
}

let empty_resp = Response {
id,
result: None,
error: None,
};

Ok(connection.sender.send(Message::Response(empty_resp))?)
send_empty_resp(connection, id, config)
}

/// Handles go to definition requests
Expand All @@ -148,6 +135,7 @@ pub fn handle_goto_def_request(
connection: &Connection,
id: RequestId,
params: &GotoDefinitionParams,
config: &Config,
text_store: &TextDocuments,
tree_store: &mut TreeStore,
) -> Result<()> {
Expand All @@ -167,13 +155,7 @@ pub fn handle_goto_def_request(
}
}

let empty_resp = Response {
id,
result: None,
error: None,
};

Ok(connection.sender.send(Message::Response(empty_resp))?)
send_empty_resp(connection, id, config)
}

/// Handles document symbols requests
Expand All @@ -189,6 +171,7 @@ pub fn handle_document_symbols_request(
connection: &Connection,
id: RequestId,
params: &DocumentSymbolParams,
config: &Config,
text_store: &TextDocuments,
tree_store: &mut TreeStore,
) -> Result<()> {
Expand All @@ -208,13 +191,7 @@ pub fn handle_document_symbols_request(
}
}

let empty_resp = Response {
id,
result: None,
error: None,
};

Ok(connection.sender.send(Message::Response(empty_resp))?)
send_empty_resp(connection, id, config)
}

/// Handles signature help requests
Expand All @@ -230,6 +207,7 @@ pub fn handle_signature_help_request(
connection: &Connection,
id: RequestId,
params: &SignatureHelpParams,
config: &Config,
text_store: &TextDocuments,
tree_store: &mut TreeStore,
names_to_instructions: &NameToInstructionMap,
Expand Down Expand Up @@ -257,13 +235,7 @@ pub fn handle_signature_help_request(
}
}

let empty_resp = Response {
id,
result: None,
error: None,
};

Ok(connection.sender.send(Message::Response(empty_resp))?)
send_empty_resp(connection, id, config)
}

/// Handles reference requests
Expand All @@ -279,6 +251,7 @@ pub fn handle_references_request(
connection: &Connection,
id: RequestId,
params: &ReferenceParams,
config: &Config,
text_store: &TextDocuments,
tree_store: &mut TreeStore,
) -> Result<()> {
Expand All @@ -299,13 +272,7 @@ pub fn handle_references_request(
}
}

let empty_resp = Response {
id,
result: None,
error: None,
};

Ok(connection.sender.send(Message::Response(empty_resp))?)
send_empty_resp(connection, id, config)
}

/// Produces diagnostics and sends a `PublishDiagnostics` notification to the client
Expand Down
27 changes: 23 additions & 4 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anyhow::{anyhow, Result};
use compile_commands::{CompilationDatabase, CompileArgs, CompileCommand, SourceFile};
use dirs::config_dir;
use log::{error, info, log, log_enabled, warn};
use lsp_server::{Connection, Message, RequestId, Response};
use lsp_textdocument::{FullTextDocument, TextDocuments};
use lsp_types::{
CompletionItem, CompletionItemKind, CompletionList, CompletionParams, CompletionTriggerKind,
Expand All @@ -27,10 +28,31 @@ use tree_sitter::InputEdit;

use crate::types::Column;
use crate::{
Arch, ArchOrAssembler, Assembler, Completable, Config, Hoverable, Instruction,
Arch, ArchOrAssembler, Assembler, Completable, Config, Hoverable, Instruction, LspClient,
NameToInstructionMap, TreeEntry, TreeStore,
};

/// Sends an empty, non-error response to the lsp client via `connection`
///
/// # Errors
///
/// Returns `Err` if the response fails to send via `connection`
pub fn send_empty_resp(connection: &Connection, id: RequestId, config: &Config) -> Result<()> {
let empty_resp = Response {
id,
result: None,
error: None,
};

// Helix shuts the server down when the above empty response is sent,
// so send nothing in its case
if config.client == Some(LspClient::Helix) {
Ok(())
} else {
Ok(connection.sender.send(Message::Response(empty_resp))?)
}
}

/// Find the start and end indices of a word inside the given line
/// Borrowed from RLS
/// characters besides the default alphanumeric and '_'
Expand Down Expand Up @@ -91,9 +113,6 @@ pub fn get_word_from_file_params(pos_params: &TextDocumentPositionParams) -> Res
}

/// Returns a string slice to the word in doc specified by the position params
///
/// The `extra_chars` param allows specifying extra chars to be considered as
/// valid word chars, in addition to the default alphanumeric, '_', and '.' chars
#[must_use]
pub fn get_word_from_pos_params<'a>(
doc: &'a FullTextDocument,
Expand Down
8 changes: 8 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -71,6 +72,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -96,6 +98,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -121,6 +124,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -146,6 +150,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -171,6 +176,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -196,6 +202,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand All @@ -221,6 +228,7 @@ mod tests {
diagnostics: None,
default_diagnostics: None,
},
client: None,
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ pub struct Config {
pub assemblers: Assemblers,
pub instruction_sets: InstructionSets,
pub opts: ConfigOptions,
pub client: Option<LspClient>,
}

impl Default for Config {
Expand All @@ -883,10 +884,16 @@ impl Default for Config {
assemblers: Assemblers::default(),
instruction_sets: InstructionSets::default(),
opts: ConfigOptions::default(),
client: None,
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum LspClient {
Helix,
}

// Instruction Set Architecture -------------------------------------------------------------------
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, EnumString, AsRefStr, Serialize, Deserialize)]
pub enum ISA {
Expand Down

0 comments on commit 96c6a7e

Please sign in to comment.