-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
288 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[workspace] | ||
members = ["markup_fmt"] | ||
members = ["dprint_plugin", "markup_fmt"] | ||
resolver = "2" | ||
|
||
[profile.release] | ||
|
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,20 @@ | ||
[package] | ||
name = "dprint_plugin_markup" | ||
version = "0.0.0" | ||
edition = "2021" | ||
authors = ["Pig Fang <[email protected]>"] | ||
description = "markup_fmt as dprint plugin." | ||
repository = "https://github.com/g-plane/markup_fmt" | ||
license = "MIT" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["rlib", "cdylib"] | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
dprint-core = { version = "0.63", default-features = false, features = [ | ||
"wasm", | ||
] } | ||
markup_fmt = { path = "../markup_fmt", features = ["config_serde"] } | ||
serde_json = "1.0" |
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,63 @@ | ||
use dprint_core::configuration::{ | ||
get_unknown_property_diagnostics, get_value, ConfigKeyMap, ConfigurationDiagnostic, | ||
GlobalConfiguration, NewLineKind, ResolveConfigurationResult, | ||
}; | ||
use markup_fmt::config::{FormatOptions, LayoutOptions, LineBreak}; | ||
|
||
pub(crate) fn resolve_config( | ||
mut config: ConfigKeyMap, | ||
global_config: &GlobalConfiguration, | ||
) -> ResolveConfigurationResult<FormatOptions> { | ||
let mut diagnostics = Vec::new(); | ||
let markup_fmt_config = FormatOptions { | ||
layout: LayoutOptions { | ||
print_width: get_value( | ||
&mut config, | ||
"printWidth", | ||
global_config.line_width.unwrap_or(80), | ||
&mut diagnostics, | ||
) as usize, | ||
use_tabs: get_value( | ||
&mut config, | ||
"useTabs", | ||
global_config.use_tabs.unwrap_or_default(), | ||
&mut diagnostics, | ||
), | ||
indent_width: get_value( | ||
&mut config, | ||
"indentWidth", | ||
global_config.indent_width.unwrap_or(2), | ||
&mut diagnostics, | ||
) as usize, | ||
line_break: match &*get_value( | ||
&mut config, | ||
"lineBreak", | ||
match global_config.new_line_kind { | ||
Some(NewLineKind::LineFeed) => "lf", | ||
Some(NewLineKind::CarriageReturnLineFeed) => "crlf", | ||
_ => "lf", | ||
} | ||
.to_string(), | ||
&mut diagnostics, | ||
) { | ||
"lf" => LineBreak::Lf, | ||
"crlf" => LineBreak::Crlf, | ||
_ => { | ||
diagnostics.push(ConfigurationDiagnostic { | ||
property_name: "lineBreak".into(), | ||
message: "invalid value for config `lineBreak`".into(), | ||
}); | ||
LineBreak::Lf | ||
} | ||
}, | ||
}, | ||
language: Default::default(), // TODO | ||
}; | ||
|
||
diagnostics.extend(get_unknown_property_diagnostics(config)); | ||
|
||
ResolveConfigurationResult { | ||
config: markup_fmt_config, | ||
diagnostics, | ||
} | ||
} |
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,99 @@ | ||
use crate::config::resolve_config; | ||
use anyhow::Result; | ||
#[cfg(target_arch = "wasm32")] | ||
use dprint_core::generate_plugin_code; | ||
use dprint_core::{ | ||
configuration::{ConfigKeyMap, GlobalConfiguration, ResolveConfigurationResult}, | ||
plugins::{FileMatchingInfo, PluginInfo, SyncPluginHandler, SyncPluginInfo}, | ||
}; | ||
use markup_fmt::{config::FormatOptions, format_text, Language}; | ||
use std::path::Path; | ||
|
||
mod config; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
type Configuration = FormatOptions; | ||
|
||
pub struct MarkupFmtPluginHandler {} | ||
|
||
impl SyncPluginHandler<FormatOptions> for MarkupFmtPluginHandler { | ||
fn plugin_info(&mut self) -> SyncPluginInfo { | ||
let version = env!("CARGO_PKG_VERSION").to_string(); | ||
SyncPluginInfo { | ||
info: PluginInfo { | ||
name: env!("CARGO_PKG_NAME").to_string(), | ||
version: version.clone(), | ||
config_key: "markup".to_string(), | ||
help_url: "https://github.com/g-plane/markup_fmt".to_string(), | ||
config_schema_url: format!( | ||
"https://plugins.dprint.dev/g-plane/markup_fmt/{}/schema.json", | ||
version | ||
), | ||
update_url: Some( | ||
"https://plugins.dprint.dev/g-plane/markup_fmt/latest.json".into(), | ||
), | ||
}, | ||
file_matching: FileMatchingInfo { | ||
file_extensions: vec!["html".into(), "vue".into(), "svelte".into()], | ||
file_names: vec![], | ||
}, | ||
} | ||
} | ||
|
||
fn license_text(&mut self) -> String { | ||
include_str!("../../LICENSE").into() | ||
} | ||
|
||
fn resolve_config( | ||
&mut self, | ||
config: ConfigKeyMap, | ||
global_config: &GlobalConfiguration, | ||
) -> ResolveConfigurationResult<FormatOptions> { | ||
resolve_config(config, global_config) | ||
} | ||
|
||
fn format( | ||
&mut self, | ||
file_path: &Path, | ||
file_text: &str, | ||
config: &FormatOptions, | ||
mut format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> Result<Option<String>>, | ||
) -> Result<Option<String>> { | ||
let language = match file_path.extension().and_then(|s| s.to_str()) { | ||
Some(ext) if ext.eq_ignore_ascii_case("html") => Language::Html, | ||
Some(ext) if ext.eq_ignore_ascii_case("vue") => Language::Vue, | ||
Some(ext) if ext.eq_ignore_ascii_case("svelte") => Language::Svelte, | ||
_ => { | ||
return Err(anyhow::anyhow!( | ||
"unknown file extension of file: {}", | ||
file_path.display() | ||
)); | ||
} | ||
}; | ||
|
||
let mut embedded_fmt_error = None; | ||
let formatted = format_text( | ||
file_text, | ||
language, | ||
config, | ||
|path, code| match format_with_host(path, code.into(), &ConfigKeyMap::new()) { | ||
Ok(Some(code)) => code.into(), | ||
Ok(None) => code.into(), | ||
Err(e) => { | ||
embedded_fmt_error = Some(e); | ||
code.into() | ||
} | ||
}, | ||
) | ||
.unwrap(); // TODO | ||
|
||
if let Some(embedded_fmt_error) = embedded_fmt_error { | ||
Err(embedded_fmt_error) | ||
} else { | ||
Ok(Some(formatted)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
generate_plugin_code!(MarkupFmtPluginHandler, MarkupFmtPluginHandler {}); |
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
Oops, something went wrong.