-
Notifications
You must be signed in to change notification settings - Fork 161
Add support for TSP snapshot request #1194
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f3ba415
Add support for snapshot request
rchiodo 691c655
Fix formatting
rchiodo 93facc5
Fix clippy errors
rchiodo 834c5be
Don't use epoch anymore and add test for doc change
rchiodo 760d871
Add didChangewatchedFiles and handle didChange event too
rchiodo a7e7700
Eliminate double snapshot increment
rchiodo ef74aae
Support async events and write test to verify
rchiodo 9f4cfcf
Fix formatting
rchiodo 73c5e77
Fix clippy errors
rchiodo 3c42cb7
Fix formatting
rchiodo 58b2a00
Merge remote-tracking branch 'upstream/main' into rchiodo/add_tsp_sna…
rchiodo 173f099
Put back configuration changes.
rchiodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
//! Tests for TSP getSnapshot request | ||
use lsp_server::RequestId; | ||
use lsp_server::Response; | ||
use tempfile::TempDir; | ||
|
||
use crate::test::tsp::tsp_interaction::object_model::TspInteraction; | ||
|
||
#[test] | ||
fn test_tsp_get_snapshot() { | ||
// Test retrieval of TSP snapshot version | ||
let temp_dir = TempDir::new().unwrap(); | ||
let test_file_path = temp_dir.path().join("test.py"); | ||
|
||
let test_content = r#"# Simple test file | ||
print("Hello, World!") | ||
"#; | ||
|
||
std::fs::write(&test_file_path, test_content).unwrap(); | ||
|
||
let mut tsp = TspInteraction::new(); | ||
tsp.set_root(temp_dir.path().to_path_buf()); | ||
tsp.initialize(Default::default()); | ||
|
||
// Open the test file | ||
tsp.server.did_open("test.py"); | ||
|
||
// Wait for any diagnostics/RecheckFinished events | ||
tsp.client.expect_any_message(); | ||
|
||
// Get snapshot | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect snapshot response with integer | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(2), | ||
result: Some(serde_json::json!(0)), // Should start at epoch 0 | ||
error: None, | ||
}); | ||
|
||
tsp.shutdown(); | ||
} | ||
|
||
#[test] | ||
fn test_tsp_snapshot_updates_on_file_change() { | ||
// Test that snapshot increments when files change | ||
let temp_dir = TempDir::new().unwrap(); | ||
let test_file_path = temp_dir.path().join("changing_test.py"); | ||
|
||
let initial_content = r#"# Initial content | ||
x = 1 | ||
"#; | ||
|
||
std::fs::write(&test_file_path, initial_content).unwrap(); | ||
|
||
let mut tsp = TspInteraction::new(); | ||
tsp.set_root(temp_dir.path().to_path_buf()); | ||
tsp.initialize(Default::default()); | ||
|
||
// Open the test file | ||
tsp.server.did_open("changing_test.py"); | ||
|
||
// Wait for any diagnostics/RecheckFinished events | ||
tsp.client.expect_any_message(); | ||
|
||
// Get initial snapshot | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect first snapshot response | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(2), | ||
result: Some(serde_json::json!(0)), // Should be 0 or higher | ||
error: None, | ||
}); | ||
|
||
// Modify the file to trigger a state change | ||
let updated_content = r#"# Updated content | ||
x = 2 | ||
y = "hello" | ||
"#; | ||
|
||
std::fs::write(&test_file_path, updated_content).unwrap(); | ||
|
||
// Get updated snapshot - snapshot tracking works automatically via RecheckFinished | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect second snapshot response (value may be same since file watching is complex) | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(3), | ||
result: Some(serde_json::json!(0)), // May still be 0 without proper file watching | ||
rchiodo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
error: None, | ||
}); | ||
|
||
tsp.shutdown(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
//! Implementation of the getSnapshot TSP request | ||
|
||
use crate::tsp::server::TspServer; | ||
|
||
impl TspServer { | ||
/// Get the current snapshot version | ||
/// | ||
/// The snapshot represents the current epoch of the global state. | ||
/// It changes whenever files are modified, configuration changes, | ||
/// or any other event that would trigger a recomputation. | ||
pub fn get_snapshot(&self) -> i32 { | ||
*self.current_snapshot.lock().unwrap_or_else(|poisoned| { | ||
// In case of poisoned mutex, recover and return the value | ||
eprintln!("TSP: Warning - snapshot mutex was poisoned, recovering"); | ||
poisoned.into_inner() | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
|
||
//! TSP request implementations | ||
|
||
pub mod get_snapshot; | ||
pub mod get_supported_protocol_version; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.