-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate kcl fmt tools to lsp. Suport formmat single file (#680)
* feat: integrate kcl fmt tools to lsp. Suport formmat single file * test: add lsp fmt uint test
- Loading branch information
Showing
6 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
use kclvm_tools::format::{format_source, FormatOptions}; | ||
use lsp_types::{Position, Range, TextEdit}; | ||
|
||
pub(crate) fn format_single_file( | ||
file: String, | ||
src: String, | ||
) -> anyhow::Result<Option<Vec<TextEdit>>> { | ||
let (source, is_formatted) = format_source( | ||
&file, | ||
&src, | ||
&FormatOptions { | ||
omit_errors: true, | ||
..Default::default() | ||
}, | ||
) | ||
.map_err(|err| anyhow::anyhow!("Formmatting failed: {}", err))?; | ||
if is_formatted { | ||
Ok(Some(vec![TextEdit { | ||
range: Range::new(Position::new(0, 0), Position::new(u32::MAX, u32::MAX)), | ||
new_text: source, | ||
}])) | ||
} else { | ||
Ok(None) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -19,6 +19,7 @@ mod state; | |
mod to_lsp; | ||
mod util; | ||
|
||
mod formatting; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
|
This file contains 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 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