Skip to content

Commit f999f17

Browse files
Waqar144ogoffart
authored andcommitted
Move slint-fmt into slint-lsp
fmt is now a part of the slint-lsp and can be invoked as: ``` slint-lsp format [OPTIONS] [FILES] ```
1 parent 70fe0df commit f999f17

14 files changed

+59
-73
lines changed

.github/workflows/ci.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,9 @@ jobs:
506506
- uses: ./.github/actions/setup-rust
507507
- name: run the formatter
508508
run: |
509-
cargo run -p slint-fmt -- -i tests/cases/*/*.slint
510-
cargo run -p slint-fmt -- -i examples/*/*.slint
511-
cargo run -p slint-fmt -- -i examples/*/*/*.slint
509+
cargo run -p slint-lsp -- format -i tests/cases/*/*.slint
510+
cargo run -p slint-lsp -- format -i examples/*/*.slint
511+
cargo run -p slint-lsp -- format -i examples/*/*/*.slint
512512
- name: Show the diff
513513
run: git diff
514514
- name: Run the intepreter test to make sure that the test are passing after format

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ members = [
5454
'tests/driver/rust',
5555
'tests/screenshots',
5656
'tools/compiler',
57-
'tools/fmt',
5857
'tools/figma_import',
5958
'tools/lsp',
6059
'tools/updater',
@@ -86,7 +85,6 @@ default-members = [
8685
'tests/driver/rust',
8786
'tests/screenshots',
8887
'tools/compiler',
89-
'tools/fmt',
9088
'tools/figma_import',
9189
'tools/lsp',
9290
'tools/viewer',

rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
edition = "2021"
12
use_small_heuristics = "Max"

tools/fmt/Cargo.toml

-30
This file was deleted.

tools/lsp/Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ preview-external = []
7979
default = ["backend-qt", "backend-winit", "renderer-femtovg", "preview"]
8080

8181
[dependencies]
82-
i-slint-compiler = { workspace = true, features = ["default"] }
82+
i-slint-compiler = { workspace = true, features = ["default", "display-diagnostics"] }
8383

8484
euclid = "0.22"
8585
itertools = { workspace = true }
@@ -96,9 +96,6 @@ i-slint-core = { workspace = true, features = ["std"], optional = true }
9696
slint = { workspace = true, features = ["compat-1-2"], optional = true }
9797
slint-interpreter = { workspace = true, features = ["compat-1-2", "highlight", "internal"], optional = true }
9898

99-
# for formatting support in lsp
100-
slint-fmt = { path = "../fmt" }
101-
10299
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
103100
clap = { version = "4.0", features = ["derive", "wrap_help"] }
104101
crossbeam-channel = "0.5" # must match the version used by lsp-server

tools/lsp/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Alternatively, you can download one of our pre-built binaries for Linux or Windo
3434

3535
As the next step, configure your editor to use the binary, no arguments are required
3636

37+
## Code formatting
38+
39+
The slint code formatting tool is part of the lsp. To learn how to use it as a standalone tool, see [fmt README](./fmt/README.md)
3740

3841
# Editor configuration
3942

tools/fmt/lib.rs tools/lsp/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright © SixtyFPS GmbH <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
3-
43
pub mod fmt;
4+
pub mod tool;
55
pub mod writer;

tools/fmt/README.md tools/lsp/fmt/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ If you find any such examples, please open an issue including the example and th
77

88
## Building
99

10-
Use `cargo build --release` or similar to build this crate.
10+
This tool is part of slint-lsp. See the README file in `lsp` for building information.
1111

1212
## Usage
1313

1414
The built binary can be used in following ways:
1515

16-
- `slint-fmt <path>` - reads the file and outputs the formatted version to stdout
17-
- `slint-fmt -i <path>` - reads the file and saves the output to the same file
18-
- `slint-fmt /dev/stdin` - using /dev/stdin you can achieve the special behavior
16+
- `slint-lsp format <path>` - reads the file and outputs the formatted version to stdout
17+
- `slint-lsp format -i <path>` - reads the file and saves the output to the same file
18+
- `slint-lsp format /dev/stdin` - using /dev/stdin you can achieve the special behavior
1919
of reading from stdin and writing to stdout
2020

2121
Note that `.slint` files are formatted, while `.md` and `.rs` files are searched for `.slint` blocks.
@@ -27,14 +27,14 @@ While we don't yet have a proper VSCode integration for this formatter,
2727
here is a simple way how you can get around it.
2828

2929
1. Install the extension Custom Format by Vehmloewff. [Marketplace link](https://marketplace.visualstudio.com/items?itemName=Vehmloewff.custom-format)
30-
2. Build slint-fmt locally.
30+
2. Build slint-lsp locally.
3131
3. Add a section like this to your vscode `settings.json`:
3232
```
3333
{
3434
"custom-format.formatters": [
3535
{
3636
"language": "slint",
37-
"command": "/path/to/your/built/slint-fmt /dev/stdin"
37+
"command": "/path/to/your/built/slint-lsp format /dev/stdin"
3838
}
3939
]
4040
}

tools/fmt/fmt.rs tools/lsp/fmt/fmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright © SixtyFPS GmbH <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
33

4-
use crate::writer::TokenWriter;
4+
use super::writer::TokenWriter;
55
use i_slint_compiler::parser::{syntax_nodes, NodeOrToken, SyntaxKind, SyntaxNode};
66

77
pub fn format_document(
@@ -780,7 +780,7 @@ fn format_state(
780780
#[cfg(test)]
781781
mod tests {
782782
use super::*;
783-
use crate::writer::FileWriter;
783+
use crate::fmt::writer::FileWriter;
784784
use i_slint_compiler::diagnostics::BuildDiagnostics;
785785
use i_slint_compiler::parser::syntax_nodes;
786786

tools/fmt/main.rs tools/lsp/fmt/tool.rs

+6-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Work in progress for a formatter.
66
Use like this to format a file:
77
```sh
8-
cargo run --bin slint-fmt -- -i some_file.slint
8+
cargo run --bin slint-lsp -- format -i some_file.slint
99
```
1010
1111
Some code in this main.rs file is duplicated with the slint-updater, i guess it could
@@ -20,29 +20,13 @@ use i_slint_compiler::parser::{syntax_nodes, SyntaxNode};
2020
use std::io::{BufWriter, Write};
2121
use std::path::Path;
2222

23-
use clap::Parser;
23+
use super::{fmt, writer};
2424

25-
mod fmt;
26-
mod writer;
27-
28-
#[derive(clap::Parser)]
29-
#[command(author, version, about, long_about = None)]
30-
struct Cli {
31-
#[arg(name = "path to .slint file(s)", action)]
32-
paths: Vec<std::path::PathBuf>,
33-
34-
/// modify the file inline instead of printing to stdout
35-
#[arg(short, long, action)]
36-
inline: bool,
37-
}
38-
39-
fn main() -> std::io::Result<()> {
40-
let args = Cli::parse();
41-
42-
for path in args.paths {
25+
pub fn run(files: Vec<std::path::PathBuf>, inplace: bool) -> std::io::Result<()> {
26+
for path in files {
4327
let source = std::fs::read_to_string(&path)?;
4428

45-
if args.inline {
29+
if inplace {
4630
let file = BufWriter::new(std::fs::File::create(&path)?);
4731
process_file(source, path, file)?
4832
} else {
@@ -129,7 +113,7 @@ fn process_file(
129113
// Formatting .60 files because of backwards compatibility (project was recently renamed)
130114
Some(ext) if ext == "slint" || ext == ".60" => process_slint_file(source, path, file),
131115
_ => {
132-
// This allows usage like `cat x.slint | slint-fmt /dev/stdin`
116+
// This allows usage like `cat x.slint | slint-lsp format /dev/stdin`
133117
if path.as_path() == Path::new("/dev/stdin") {
134118
return process_slint_file(source, path, file);
135119
}
File renamed without changes.

tools/lsp/language/formatting.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// Copyright © SixtyFPS GmbH <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
13
use super::DocumentCache;
4+
use crate::fmt::{fmt, writer};
25
use crate::util::map_range;
36
use dissimilar::Chunk;
47
use i_slint_compiler::parser::SyntaxToken;
@@ -9,7 +12,7 @@ struct StringWriter {
912
text: String,
1013
}
1114

12-
impl slint_fmt::writer::TokenWriter for StringWriter {
15+
impl writer::TokenWriter for StringWriter {
1316
fn no_change(&mut self, token: SyntaxToken) -> std::io::Result<()> {
1417
self.text += &token.text();
1518
Ok(())
@@ -36,7 +39,7 @@ pub fn format_document(
3639
let doc = doc.node.as_ref()?;
3740

3841
let mut writer = StringWriter { text: String::new() };
39-
slint_fmt::fmt::format_document(doc.clone(), &mut writer).ok()?;
42+
fmt::format_document(doc.clone(), &mut writer).ok()?;
4043

4144
let original: String = doc.text().into();
4245
let diff = dissimilar::diff(&original, &writer.text);

tools/lsp/main.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
compile_error!("Feature preview-engine and preview-builtin need to be enabled together when building native LSP");
88

99
mod common;
10+
mod fmt;
1011
mod language;
1112
pub mod lsp_ext;
1213
#[cfg(feature = "preview-engine")]
@@ -22,7 +23,7 @@ use lsp_types::notification::{
2223
};
2324
use lsp_types::{DidChangeTextDocumentParams, DidOpenTextDocumentParams, InitializeParams, Url};
2425

25-
use clap::Parser;
26+
use clap::{Args, Parser, Subcommand};
2627
use lsp_server::{Connection, ErrorCode, IoThreads, Message, RequestId, Response};
2728
use std::cell::RefCell;
2829
use std::collections::HashMap;
@@ -58,6 +59,25 @@ pub struct Cli {
5859
/// Hide the preview toolbar
5960
#[arg(long, action)]
6061
no_toolbar: bool,
62+
63+
#[command(subcommand)]
64+
command: Option<Commands>,
65+
}
66+
67+
#[derive(Subcommand, Clone)]
68+
enum Commands {
69+
/// Format slint files
70+
Format(Format),
71+
}
72+
73+
#[derive(Args, Clone)]
74+
struct Format {
75+
#[arg(name = "path to .slint file(s)", action)]
76+
paths: Vec<std::path::PathBuf>,
77+
78+
/// modify the file inline instead of printing to stdout
79+
#[arg(short, long, action)]
80+
inline: bool,
6181
}
6282

6383
enum OutgoingRequest {
@@ -156,6 +176,14 @@ fn main() {
156176
std::env::set_var("SLINT_BACKEND", &args.backend);
157177
}
158178

179+
if let Some(Commands::Format(args)) = args.command {
180+
let _ = fmt::tool::run(args.paths, args.inline).map_err(|e| {
181+
eprintln!("{e}");
182+
std::process::exit(1);
183+
});
184+
std::process::exit(0);
185+
}
186+
159187
#[cfg(feature = "preview-engine")]
160188
{
161189
let cli_args = args.clone();
@@ -187,6 +215,7 @@ fn main() {
187215
preview::start_ui_event_loop(cli_args);
188216
lsp_thread.join().unwrap();
189217
}
218+
190219
#[cfg(not(feature = "preview-engine"))]
191220
match run_lsp_server(args) {
192221
Ok(threads) => threads.join().unwrap(),

tools/lsp/wasm_main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![cfg(target_arch = "wasm32")]
55

66
mod common;
7+
mod fmt;
78
mod language;
89
pub mod lsp_ext;
910
#[cfg(feature = "preview-engine")]

0 commit comments

Comments
 (0)