Skip to content

Commit 1190d95

Browse files
authored
Fix LSP initialisation bug. (#5771)
## Description I noticed that LSP wasn't loading the correct syntax highlighting and that other LSP features like go_to, hover etc.. were not working when a new project was loaded. It would start working after the first key press though. We now wait for parsing on the code_action LSP feature. We also enable the wait_for_parsing function to wait until the compilation thread is started so it doesn't return early in the case where the LSP requests were received by the server before the did_open notification. before ![Screenshot 2024-03-26 at 11 36 54 am](https://github.com/FuelLabs/sway/assets/1289413/807055fd-12a5-4d14-b165-10a41bd17187) after ![Screenshot 2024-03-26 at 11 35 39 am](https://github.com/FuelLabs/sway/assets/1289413/f7a440a2-eb0b-48e1-ba82-c78f5e5fa252)
1 parent 2eb4ecc commit 1190d95

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

sway-lsp/src/handlers/request.rs

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ pub async fn handle_code_action(
240240
state: &ServerState,
241241
params: lsp_types::CodeActionParams,
242242
) -> Result<Option<lsp_types::CodeActionResponse>> {
243+
let _ = state.wait_for_parsing().await;
243244
match state
244245
.sessions
245246
.uri_and_session_from_workspace(&params.text_document.uri)

sway-lsp/src/server_state.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ impl ServerState {
205205
/// this process until `is_compiling` becomes false.
206206
pub async fn wait_for_parsing(&self) {
207207
loop {
208-
if !self.is_compiling.load(Ordering::SeqCst) {
208+
// Check both the is_compiling flag and the last_compilation_state.
209+
// Wait if is_compiling is true or if the last_compilation_state is Uninitialized.
210+
if !self.is_compiling.load(Ordering::SeqCst)
211+
&& *self.last_compilation_state.read() != LastCompilationState::Uninitialized
212+
{
209213
// compilation is finished, lets check if there are pending compilation requests.
210214
if self.cb_rx.is_empty() {
211215
// no pending compilation work, safe to break.
@@ -225,7 +229,6 @@ impl ServerState {
225229

226230
// Set the retrigger_compilation flag to true so that the compilation exits early
227231
self.retrigger_compilation.store(true, Ordering::SeqCst);
228-
self.wait_for_parsing().await;
229232

230233
// Send a terminate message to the compilation thread
231234
self.cb_tx

sway-lsp/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn did_change_stress_test() {
209209
#[test]
210210
fn did_change_stress_test_random_wait() {
211211
run_async!({
212-
let test_duration = tokio::time::Duration::from_secs(12 * 60); // 5 minutes timeout
212+
let test_duration = tokio::time::Duration::from_secs(5 * 60); // 5 minutes timeout
213213
let test_future = async {
214214
std::env::set_var("RUST_BACKTRACE", "1");
215215
let default_panic = std::panic::take_hook();

0 commit comments

Comments
 (0)