Skip to content
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

fix formatting of unsaved changes in LSP #1173

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,21 @@ pub(crate) fn handle_semantic_tokens_full(
}

pub(crate) fn handle_formatting(
_snapshot: LanguageServerSnapshot,
snapshot: LanguageServerSnapshot,
params: lsp_types::DocumentFormattingParams,
_sender: Sender<Task>,
) -> anyhow::Result<Option<Vec<TextEdit>>> {
let file = file_path_from_url(&params.text_document.uri)?;
let src = std::fs::read_to_string(file.clone())?;
let path = from_lsp::abs_path(&params.text_document.uri)?;
let src = {
let vfs = snapshot.vfs.read();
let file_id = vfs
.file_id(&path.into())
.ok_or(anyhow::anyhow!("Already checked that the file_id exists!"))?;

String::from_utf8(vfs.file_contents(file_id).to_vec())?
};

format(file, src, None)
}

Expand Down
71 changes: 71 additions & 0 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,77 @@ fn formatting_test() {
)
}

#[test]
fn formatting_unsaved_test() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut path = root.clone();

path.push("src/test_data/format/format_range.k");

let path = path.to_str().unwrap();
let src = std::fs::read_to_string(path).unwrap();
let server = Project {}.server(InitializeParams::default());

let uri = Url::from_file_path(path).unwrap();

// Mock open file
server.notification::<lsp_types::notification::DidOpenTextDocument>(
lsp_types::DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: uri.clone(),
language_id: "KCL".to_string(),
version: 0,
text: src,
},
},
);

// Mock edit file
server.notification::<lsp_types::notification::DidChangeTextDocument>(
lsp_types::DidChangeTextDocumentParams {
text_document: lsp_types::VersionedTextDocumentIdentifier {
uri: uri.clone(),
version: 1,
},
content_changes: vec![lsp_types::TextDocumentContentChangeEvent {
range: Some(lsp_types::Range::new(
lsp_types::Position::new(0, 0),
lsp_types::Position::new(0, 0),
)),
range_length: Some(0),
text: String::from("unsaved = 0\n"),
}],
},
);

let id = server.next_request_id.get();
server.next_request_id.set(id.wrapping_add(1));

let r: Request = Request::new(
id.into(),
"textDocument/formatting".to_string(),
DocumentFormattingParams {
text_document: TextDocumentIdentifier {
uri: Url::from_file_path(path).unwrap(),
},
options: Default::default(),
work_done_progress_params: Default::default(),
},
);

// Send request and wait for it's response
let res = server.send_and_receive(r);

assert_eq!(
res.result.unwrap(),
to_json(Some(vec![TextEdit {
range: Range::new(Position::new(0, 0), Position::new(u32::MAX, u32::MAX),),
new_text: "unsaved = 0\na = 1\nb = 2\nc = 3\nd = 4\n".to_string()
}]))
.unwrap()
)
}

// Integration testing of lsp and konfig
fn konfig_path() -> PathBuf {
let konfig_path = Path::new(".")
Expand Down
Loading