Skip to content
This repository was archived by the owner on Dec 17, 2022. It is now read-only.

Commit 29c8631

Browse files
committed
chore: Split up wasm code from the rest.
1 parent 02ea8ec commit 29c8631

File tree

5 files changed

+158
-137
lines changed

5 files changed

+158
-137
lines changed

.github/workflows/ci.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ jobs:
2020

2121
steps:
2222
- uses: actions/checkout@v2
23-
- name: Install wasm32 target
24-
if: matrix.config.kind == 'test_release'
25-
run: rustup target add wasm32-unknown-unknown
2623

2724
- name: Install and use nightly compiler
2825
run: rustup default nightly
2926

27+
- name: Install wasm32 target
28+
if: matrix.config.kind == 'test_release'
29+
run: rustup target add wasm32-unknown-unknown --toolchain nightly
30+
3031
- name: Run setup script
3132
run: |
3233
chmod +x setup.sh

src/config.rs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use rustfmt_nightly::{Config, NewlineStyle, EmitMode, Edition};
2+
3+
use std::collections::HashMap;
4+
use serde::{Serialize, Deserialize};
5+
use dprint_core::configuration::{GlobalConfiguration, ResolveConfigurationResult, NewLineKind, ConfigurationDiagnostic};
6+
7+
#[derive(Clone, Serialize, Deserialize)]
8+
pub struct Configuration {
9+
// Unfortunately no resolved configuration at the moment because serializing
10+
// rustfmt's PartialConfig configuration kept causing a panic
11+
#[serde(flatten)]
12+
pub(crate) config: HashMap<String, String>,
13+
#[serde(skip_serializing, skip_deserializing)]
14+
pub(crate) rustfmt_config: Config,
15+
}
16+
17+
pub fn resolve_config(
18+
config: HashMap<String, String>,
19+
global_config: &GlobalConfiguration,
20+
) -> ResolveConfigurationResult<Configuration> {
21+
let mut rustfmt_config = Config::default();
22+
let mut diagnostics = Vec::new();
23+
24+
rustfmt_config.set().edition(Edition::Edition2018);
25+
26+
// set dprint global configuration
27+
if let Some(line_width) = global_config.line_width {
28+
rustfmt_config.set().max_width(line_width as usize);
29+
}
30+
if let Some(use_tabs) = global_config.use_tabs {
31+
rustfmt_config.set().hard_tabs(use_tabs);
32+
}
33+
if let Some(indent_width) = global_config.indent_width {
34+
rustfmt_config.set().tab_spaces(indent_width as usize);
35+
}
36+
if let Some(new_line_kind) = global_config.new_line_kind {
37+
rustfmt_config.set().newline_style(match new_line_kind {
38+
NewLineKind::Auto => NewlineStyle::Auto,
39+
NewLineKind::LineFeed => NewlineStyle::Unix,
40+
NewLineKind::CarriageReturnLineFeed => NewlineStyle::Windows,
41+
NewLineKind::System => NewlineStyle::Native,
42+
});
43+
}
44+
45+
for (key, value) in config.iter() {
46+
if key == "newLineKind" {
47+
match value.as_str() {
48+
"auto" => rustfmt_config.set().newline_style(NewlineStyle::Auto),
49+
"lf" => rustfmt_config.set().newline_style(NewlineStyle::Unix),
50+
"crlf" => rustfmt_config.set().newline_style(NewlineStyle::Windows),
51+
"system" => rustfmt_config.set().newline_style(NewlineStyle::Native),
52+
_ => {
53+
diagnostics.push(ConfigurationDiagnostic {
54+
property_name: String::from(key),
55+
message: format!("Invalid newline kind: {}", value),
56+
});
57+
}
58+
}
59+
continue;
60+
}
61+
62+
let key = match key.as_str() {
63+
"lineWidth" => "max_width",
64+
"useTabs" => "hard_tabs",
65+
"indentWidth" => "tab_spaces",
66+
_ => key,
67+
};
68+
if Config::is_valid_key_val(key, value) {
69+
rustfmt_config.override_value(key, value);
70+
} else {
71+
let message = format!("Invalid key or value in configuration. Key: {}, Value: {}", key, value);
72+
diagnostics.push(ConfigurationDiagnostic {
73+
property_name: String::from(key),
74+
message,
75+
});
76+
}
77+
}
78+
79+
rustfmt_config.set().emit_mode(EmitMode::Stdout);
80+
81+
ResolveConfigurationResult {
82+
diagnostics,
83+
config: Configuration { config, rustfmt_config },
84+
}
85+
}

src/format.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use rustfmt_nightly::{Input, Session};
2+
use super::config::{Configuration};
3+
4+
pub fn format_text(
5+
file_text: &str,
6+
config: &Configuration,
7+
) -> Result<String, String> {
8+
let mut out = Vec::new();
9+
{
10+
let input = Input::Text(String::from(file_text));
11+
let mut session = Session::new(config.rustfmt_config.clone(), Some(&mut out));
12+
match session.format(input) {
13+
Err(err) => {
14+
return Err(err.to_string());
15+
},
16+
_ => {
17+
// do nothing
18+
}
19+
}
20+
}
21+
22+
// rustfmt adds this prefix, so just ignore it
23+
let prefix = "stdin:\n\n";
24+
Ok(String::from(std::str::from_utf8(&out[prefix.len()..]).unwrap()))
25+
}

src/lib.rs

+7-134
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,10 @@
1-
use rustfmt_nightly::{Input, Session, Config, NewlineStyle, EmitMode, Edition};
1+
pub mod config;
2+
mod format;
23

3-
use std::path::PathBuf;
4-
use std::collections::HashMap;
5-
use serde::{Serialize, Deserialize};
6-
use dprint_core::generate_plugin_code;
7-
use dprint_core::configuration::{GlobalConfiguration, ResolveConfigurationResult, NewLineKind, ConfigurationDiagnostic};
4+
pub use format::*;
85

9-
#[derive(Clone, Serialize, Deserialize)]
10-
struct Configuration {
11-
// Unfortunately no resolved configuration at the moment because serializing
12-
// rustfmt's PartialConfig configuration kept causing a panic
13-
#[serde(flatten)]
14-
config: HashMap<String, String>,
15-
#[serde(skip_serializing, skip_deserializing)]
16-
rustfmt_config: Config,
17-
}
6+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
7+
mod wasm_plugin;
188

19-
fn resolve_config(
20-
config: HashMap<String, String>,
21-
global_config: &GlobalConfiguration,
22-
) -> ResolveConfigurationResult<Configuration> {
23-
let mut rustfmt_config = Config::default();
24-
let mut diagnostics = Vec::new();
25-
26-
rustfmt_config.set().edition(Edition::Edition2018);
27-
28-
// set dprint global configuration
29-
if let Some(line_width) = global_config.line_width {
30-
rustfmt_config.set().max_width(line_width as usize);
31-
}
32-
if let Some(use_tabs) = global_config.use_tabs {
33-
rustfmt_config.set().hard_tabs(use_tabs);
34-
}
35-
if let Some(indent_width) = global_config.indent_width {
36-
rustfmt_config.set().tab_spaces(indent_width as usize);
37-
}
38-
if let Some(new_line_kind) = global_config.new_line_kind {
39-
rustfmt_config.set().newline_style(match new_line_kind {
40-
NewLineKind::Auto => NewlineStyle::Auto,
41-
NewLineKind::LineFeed => NewlineStyle::Unix,
42-
NewLineKind::CarriageReturnLineFeed => NewlineStyle::Windows,
43-
NewLineKind::System => NewlineStyle::Native,
44-
});
45-
}
46-
47-
for (key, value) in config.iter() {
48-
if key == "newLineKind" {
49-
match value.as_str() {
50-
"auto" => rustfmt_config.set().newline_style(NewlineStyle::Auto),
51-
"lf" => rustfmt_config.set().newline_style(NewlineStyle::Unix),
52-
"crlf" => rustfmt_config.set().newline_style(NewlineStyle::Windows),
53-
"system" => rustfmt_config.set().newline_style(NewlineStyle::Native),
54-
_ => {
55-
diagnostics.push(ConfigurationDiagnostic {
56-
property_name: String::from(key),
57-
message: format!("Invalid newline kind: {}", value),
58-
});
59-
}
60-
}
61-
continue;
62-
}
63-
64-
let key = match key.as_str() {
65-
"lineWidth" => "max_width",
66-
"useTabs" => "hard_tabs",
67-
"indentWidth" => "tab_spaces",
68-
_ => key,
69-
};
70-
if Config::is_valid_key_val(key, value) {
71-
rustfmt_config.override_value(key, value);
72-
} else {
73-
let message = format!("Invalid key or value in configuration. Key: {}, Value: {}", key, value);
74-
diagnostics.push(ConfigurationDiagnostic {
75-
property_name: String::from(key),
76-
message,
77-
});
78-
}
79-
}
80-
81-
rustfmt_config.set().emit_mode(EmitMode::Stdout);
82-
83-
ResolveConfigurationResult {
84-
diagnostics,
85-
config: Configuration { config, rustfmt_config },
86-
}
87-
}
88-
89-
fn get_plugin_config_key() -> String {
90-
// return the JSON object key name used in the configuration file
91-
String::from("rustfmt")
92-
}
93-
94-
fn get_plugin_file_extensions() -> Vec<String> {
95-
vec![String::from("rs")]
96-
}
97-
98-
fn get_plugin_help_url() -> String {
99-
String::from("https://dprint.dev/plugins/rustfmt")
100-
}
101-
102-
fn get_plugin_config_schema_url() -> String {
103-
// for now, return an empty string. Return a schema url once VSCode
104-
// supports $schema properties in descendant objects:
105-
// https://github.com/microsoft/vscode/issues/98443
106-
String::new()
107-
}
108-
109-
fn get_plugin_license_text() -> String {
110-
std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into()
111-
}
112-
113-
fn format_text(
114-
_: &PathBuf,
115-
file_text: &str,
116-
config: &Configuration,
117-
) -> Result<String, String> {
118-
let mut out = Vec::new();
119-
{
120-
let input = Input::Text(String::from(file_text));
121-
let mut session = Session::new(config.rustfmt_config.clone(), Some(&mut out));
122-
match session.format(input) {
123-
Err(err) => {
124-
return Err(err.to_string());
125-
},
126-
_ => {
127-
// do nothing
128-
}
129-
}
130-
}
131-
132-
// rustfmt adds this prefix, so just ignore it
133-
let prefix = "stdin:\n\n";
134-
Ok(String::from(std::str::from_utf8(&out[prefix.len()..]).unwrap()))
135-
}
136-
137-
generate_plugin_code!();
9+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
10+
pub use wasm_plugin::*;

src/wasm_plugin.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::path::PathBuf;
2+
use dprint_core::generate_plugin_code;
3+
use super::config::{Configuration, resolve_config};
4+
5+
fn get_plugin_config_key() -> String {
6+
// return the JSON object key name used in the configuration file
7+
String::from("rustfmt")
8+
}
9+
10+
fn get_plugin_file_extensions() -> Vec<String> {
11+
vec![String::from("rs")]
12+
}
13+
14+
fn get_plugin_help_url() -> String {
15+
String::from("https://dprint.dev/plugins/rustfmt")
16+
}
17+
18+
fn get_plugin_config_schema_url() -> String {
19+
// for now, return an empty string. Return a schema url once VSCode
20+
// supports $schema properties in descendant objects:
21+
// https://github.com/microsoft/vscode/issues/98443
22+
String::new()
23+
}
24+
25+
fn get_plugin_license_text() -> String {
26+
std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into()
27+
}
28+
29+
fn format_text(
30+
_: &PathBuf,
31+
file_text: &str,
32+
config: &Configuration,
33+
) -> Result<String, String> {
34+
super::format_text(file_text, config)
35+
}
36+
37+
generate_plugin_code!();

0 commit comments

Comments
 (0)