Skip to content

Commit

Permalink
Fixed LSP hanging in release by doing only TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Oct 23, 2023
1 parent 12cf0b0 commit 44437f0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 50 deletions.
49 changes: 15 additions & 34 deletions src/dev_aid/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

use std::{error::Error, net::SocketAddr};
use std::fs::File;
use lsp_types::{*, request::Request, notification::*};

use lsp_server::{Response, Message, Connection};
Expand All @@ -11,29 +10,6 @@ use crate::{parser::perform_full_semantic_parse, dev_aid::syntax_highlighting::c

use super::syntax_highlighting::{IDETokenType, IDEIdentifierType, IDEToken};

static LSP_LOG_PATH : &str = if crate::tokenizer::const_eq_str(std::env::consts::OS, "windows") {
"C:\\Users\\lenna\\lsp_out.txt"
} else {
"/home/lennart/lsp_out.txt"
};

thread_local!(static LSP_LOG: File = File::create(LSP_LOG_PATH).expect("Replacement terminal /home/lennart/lsp_out.txt could not be created"));

macro_rules! println {
($($arg:tt)*) => {{
use std::io::Write;
LSP_LOG.with(|mut file| {
write!(file, $($arg)*).unwrap();
write!(file, "\n").unwrap();
})
}};
}
/*macro_rules! println {
($($arg:tt)*) => {{
eprintln!($($arg)*);
}};
}*/

struct LoadedFileCache {
linker : Linker,
uris : ArenaVector<Url, FileUUIDMarker>
Expand All @@ -49,9 +25,17 @@ impl LoadedFileCache {
.map(|(uuid, _uri_found)| uuid)
}
fn update_text(&mut self, uri : Url, new_file_text : String) {
let file_uuid = self.find_uri(&uri).unwrap();
let found_opt = self.find_uri(&uri);
let found_opt_was_none = found_opt.is_none();
let file_uuid : FileUUID = found_opt.unwrap_or_else(|| self.linker.reserve_file());
let (full_parse, parsing_errors) = perform_full_semantic_parse(new_file_text, file_uuid);
self.linker.relink(file_uuid, full_parse, parsing_errors);

if found_opt_was_none {
self.linker.add_reserved_file(file_uuid, full_parse, parsing_errors);
self.uris.insert(file_uuid, uri.clone());
} else {
self.linker.relink(file_uuid, full_parse, parsing_errors);
}
}
fn ensure_contains_file(&mut self, uri : &Url) -> FileUUID {
if let Some(found) = self.find_uri(uri) {
Expand All @@ -67,7 +51,7 @@ impl LoadedFileCache {
}
}

pub fn lsp_main(use_tcp : Option<u16>) -> Result<(), Box<dyn Error + Sync + Send>> {
pub fn lsp_main(port : u16) -> Result<(), Box<dyn Error + Sync + Send>> {
// Note that we must have our logging only write out to stderr.
//println!("starting generic LSP server");

Expand All @@ -76,14 +60,10 @@ pub fn lsp_main(use_tcp : Option<u16>) -> Result<(), Box<dyn Error + Sync + Send
// Create the transport. Includes the stdio (stdin and stdout) versions but this could
// also be implemented to use sockets or HTTP.
//let (connection, io_threads) = Connection::listen(SocketAddr::from(([127,0,0,1], 25000)))?;
let (connection, io_threads) = if let Some(port) = use_tcp {
println!("Listening for connections on port {}...", port);
Connection::connect(SocketAddr::from(([127,0,0,1], port)))?
} else {
Connection::stdio()
};
println!("Connecting on port {}...", port);
let (connection, io_threads) = Connection::connect(SocketAddr::from(([127,0,0,1], port)))?;
println!("connection established");

// Run the server and wait for the two threads to end (typically by trigger LSP Exit event).
let server_capabilities = serde_json::to_value(&ServerCapabilities {
definition_provider: Some(OneOf::Left(true)),
Expand Down Expand Up @@ -352,6 +332,7 @@ fn main_loop(
let mut errors = file_cache.linker.files[uuid].parsing_errors.clone();
file_cache.linker.get_linking_errors(uuid, &mut errors);

println!("Flattening...");
file_cache.linker.flatten_all_modules_in_file(uuid, &mut errors);

println!("Errors: {:?}", &errors);
Expand Down
2 changes: 1 addition & 1 deletion src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub fn syntax_highlight_file(file_paths : Vec<PathBuf>) {
linker.flatten_all_modules_in_file(file_uuid, &mut errors);

for err in errors.errors {
// err.pretty_print_error(f.parsing_errors.file, &token_offsets, &paths_arena, &mut file_cache);
err.pretty_print_error(f.parsing_errors.file, &token_offsets, &paths_arena, &mut file_cache);
}
}
}
15 changes: 9 additions & 6 deletions src/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
}
}
fn flatten_code(&mut self, code : &CodeBlock, condition : WireID) {
for (stmt, _stmt_span) in &code.statements {
for (stmt, stmt_span) in &code.statements {
match stmt {
Statement::Declaration{local_id} => {
// TODO
Expand Down Expand Up @@ -350,11 +350,14 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
}
},
Statement::Assign{to, expr : non_func_expr, eq_sign_position : _} => {
assert!(to.len() == 1);
let Some(read_side) = self.flatten_single_expr(non_func_expr, condition) else {return;};
let t = &to[0];
let Some(write_side) = self.flatten_assignable_expr(&t.expr, condition) else {return;};
self.connections.push(Connection{num_regs : t.num_regs, from: read_side, to: write_side, condition});
if to.len() == 1 {
let Some(read_side) = self.flatten_single_expr(non_func_expr, condition) else {return;};
let t = &to[0];
let Some(write_side) = self.flatten_assignable_expr(&t.expr, condition) else {return;};
self.connections.push(Connection{num_regs : t.num_regs, from: read_side, to: write_side, condition});
} else {
self.errors.error_basic(*stmt_span, format!("Non-function assignments must only output exactly 1 instead of {}", to.len()));
}
},
Statement::Block(inner_code) => {
self.flatten_code(inner_code, condition);
Expand Down
1 change: 1 addition & 0 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ impl Linker {
pub fn flatten_all_modules_in_file(&self, file : FileUUID, errors : &mut ErrorCollector) {
for md_uuid in &self.files[file].associated_values {
let named = &self.links.globals[*md_uuid];
eprintln!("Flattening {}", named.get_name(self));
if let Named::Module(md) = named {
if !md.link_info.is_fully_linked {
continue;
Expand Down
13 changes: 4 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,20 @@ fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
let _executable_path = args.next();

let mut file_paths : Vec<PathBuf> = Vec::new();
let mut is_lsp = true;
let mut is_lsp_tcp = true;
/*for arg in args {
let mut is_lsp = false;
for arg in args {
match arg.as_str() {
"--lsp" => {
is_lsp = true;
},
"--lsp-tcp" => {
is_lsp = true;
is_lsp_tcp = true;
}
other => {
file_paths.push(PathBuf::from(other));
}
}
}*/
}
#[cfg(feature = "lsp")]
if is_lsp {
return dev_aid::lsp::lsp_main(if is_lsp_tcp {Some(25000)} else {None});
return dev_aid::lsp::lsp_main(25000);
}
if file_paths.len() == 0 {
// Quick debug file
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
}).collect();
let end_at = value.1.1;
statements.push((Statement::Assign{to : converted_left, eq_sign_position : Some(assign_pos), expr : value}, Span(start_at, end_at)));

self.eat_plain(token_stream, kw(";"), "right-hand side of expression")?;
Some(())
} else {
Expand Down

0 comments on commit 44437f0

Please sign in to comment.