Skip to content

Commit

Permalink
Feature/lsp (#37)
Browse files Browse the repository at this point in the history
* language serverに対応中

* rename api name

* remove format_lsp.rs

* option の変更によるテストの修正

* Fix comments format

* format
  • Loading branch information
usagrada authored Jan 25, 2023
1 parent 35c3551 commit 141c95e
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 84 deletions.
440 changes: 439 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/usagrada/"
satysfi-parser = { git = "https://github.com/usagrada/satysfi-parser.git" }
clap = { version = "3", features = ["derive"] }
dirs = "*"
lspower = "1.4.0"

[[bin]]
name = "satysfi-fmt"
Expand Down
2 changes: 1 addition & 1 deletion src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn cst_insert_comment(cst: &mut Cst, comments: &mut VecDeque<Comment>) {
pub fn to_comment_string(text: String) -> String {
let index = text.find('%').unwrap();
let comment = text[index + 1..].trim_end();
if comment.len() == 0 || comment.starts_with(char::is_whitespace) || comment.starts_with('%') {
if comment.is_empty() || comment.starts_with(char::is_whitespace) || comment.starts_with('%') {
// 空白or複数の%で始まっていたらそのまま表示
format!("%{}", comment)
} else {
Expand Down
145 changes: 91 additions & 54 deletions src/formatter.rs

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[inline]
pub fn indent_space(depth: usize) -> String {
let mut result = String::new();
for _ in 0..depth {
result.push(' ');
}
result
}

#[inline]
pub fn indent_tab(depth: usize) -> String {
"\t".repeat(depth)
}
62 changes: 42 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
mod comment;
mod formatter;
mod helper;
mod reserved_words;
#[cfg(test)]
mod tests;
mod visualize;

use comment::*;
use formatter::Formatter;
use lspower::lsp::{FormattingOptions, TextEdit};
use satysfi_parser::{grammar, CstText};
pub use visualize::*;

pub struct OptionData {
pub row_length: usize,
pub indent_space: usize,
pub command_args_space: bool,
}

impl Default for OptionData {
fn default() -> Self {
Self {
row_length: 80,
indent_space: 4,
command_args_space: true,
}
}
}

/// satysfi の文字列を渡すと format したものを返す
/// * `input` - satysfi のコード
/// * `output` - format された文字列
pub fn format(input: &str, option: OptionData) -> String {
pub fn format(input: &str, option: FormattingOptions) -> String {
/*
CstText {
text: string,
Expand All @@ -42,7 +28,10 @@ pub fn format(input: &str, option: OptionData) -> String {
let err = csttext.unwrap_err();
let line = err.0.line;
let col = err.0.column;
eprintln!("disable to format\n[parse error] line: {}, column: {}", line, col);
eprintln!(
"disable to format\n[parse error] line: {}, column: {}",
line, col
);
return input.to_string();
}
let csttext = csttext.unwrap();
Expand All @@ -53,7 +42,40 @@ pub fn format(input: &str, option: OptionData) -> String {
visualize_csttext_tree(&csttext);

let depth = 0;
let output = formatter.format(input, &csttext.cst, depth);


formatter.format(input, &csttext.cst, depth)
}

output
pub fn formatting(input: &str, option: FormattingOptions) -> Vec<TextEdit> {
let csttext = CstText::parse(input, grammar::program);
if csttext.is_err() {
let err = csttext.unwrap_err();
let line = err.0.line;
let col = err.0.column;
eprintln!(
"disable to format\n[parse error] line: {}, column: {}",
line, col
);
return Vec::new();
}
let csttext = csttext.unwrap();
let csttext = csttext_insert_comments(csttext);
let formatter = Formatter::new(&csttext, option);
let output = formatter.format(input, &csttext.cst, 0);
let mut edits = Vec::new();
edits.push(TextEdit {
range: lspower::lsp::Range {
start: lspower::lsp::Position {
line: 0,
character: 0,
},
end: lspower::lsp::Position {
line: csttext.lines.len() as u32,
character: csttext.text.split('\n').last().unwrap().len() as u32,
},
},
new_text: output,
});
edits
}
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use satysfi_formatter::{format, OptionData};
use lspower::lsp::FormattingOptions;
use satysfi_formatter::format;
use std::{fs, path::PathBuf};

#[derive(Parser, Debug)]
Expand All @@ -25,16 +26,16 @@ struct Cli {
fn main() {
let cli = Cli::parse();
let code = fs::read_to_string(&cli.file).expect("Failed to read file");
let option = OptionData {
indent_space: cli.indent_space,
command_args_space: cli.cspace,
let option = FormattingOptions {
tab_size: cli.indent_space as u32,
..Default::default()
};
let output = format(&code, option);

match (cli.output, cli.write) {
(Some(path), _) => fs::write(&path, output).expect("Failed to write file"),
(None, true) => fs::write(&cli.file, output).expect("Failed to write file"),
(Some(path), _) => fs::write(path, &output).expect("Failed to write file"),
(None, true) => fs::write(&cli.file, &output).expect("Failed to write file"),
(None, false) => println!("{}", output),
}
println!("{:?}", output)
}
25 changes: 25 additions & 0 deletions src/tests/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,28 @@ document(|title = { hello }|)'<
"#;
test_tmpl(text, expect)
}

#[test]
fn test_comment8() {
let text = r#"@import: hello
@require: local %comment
document(|title = {hello}|)'<
+section{hello}<%
+p{
hello
}
>%
>"#;

let expect = r#"@import: hello
@require: local %comment
document(|title = { hello }|)'<
+section { hello } <%
+p { hello }
>%
>
"#;
test_tmpl(text, expect)
}
9 changes: 7 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{format, OptionData};
use crate::format;
use lspower::lsp::FormattingOptions;

mod comment;
mod common;
Expand All @@ -10,7 +11,11 @@ mod module;
mod space;

fn test_tmpl(input: &str, expect: &str) {
let option = OptionData::default();
let option = FormattingOptions {
tab_size: 4,
insert_spaces: true,
..Default::default()
};
let output = format(input, option);
assert_eq!(output, expect);
}
Expand Down

0 comments on commit 141c95e

Please sign in to comment.