-
Notifications
You must be signed in to change notification settings - Fork 3
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
25 changed files
with
731 additions
and
67 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "dprint-plugin-dockerfile" | ||
version = "0.0.1" | ||
version = "0.1.0" | ||
authors = ["David Sherret <[email protected]>"] | ||
edition = "2018" | ||
homepage = "https://github.com/dprint/dprint-plugin-dockerfile" | ||
|
@@ -25,8 +25,8 @@ wasm = ["serde_json", "dprint-core/wasm"] | |
tracing = ["dprint-core/tracing"] | ||
|
||
[dependencies] | ||
dockerfile-parser = "0.7.1" | ||
dprint-core = { version = "0.44.0", features = ["formatting"] } | ||
dockerfile-parser = { git = "https://github.com/dsherret/dockerfile-parser-rs", branch = "span-for-nodes" } | ||
dprint-core = { version = "0.46.4", features = ["formatting"] } | ||
serde = { version = "1.0.88", features = ["derive"] } | ||
serde_json = { version = "1.0", optional = true } | ||
|
||
|
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,3 +1,7 @@ | ||
# dprint-plugin-dockerfile | ||
|
||
WIP - Future home of a dockerfile formatter. | ||
Dockerfile code formatter plugin for [dprint](https://dprint.dev). | ||
|
||
This uses [dockerfile-parser-rs](https://github.com/dsherret/dockerfile-parser-rs) to parse a dockerfile. | ||
|
||
See [releases](https://github.com/dprint/dprint-plugin-dockerfile/releases/) for usage. |
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
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
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,9 +1,37 @@ | ||
use dockerfile_parser::Dockerfile; | ||
use dprint_core::configuration::resolve_new_line_kind; | ||
use dprint_core::formatting::PrintOptions; | ||
use dprint_core::types::ErrBox; | ||
use std::path::Path; | ||
|
||
use crate::configuration::Configuration; | ||
use crate::parser::parse_items; | ||
|
||
pub fn format_text(_file_path: &Path, text: &str, _config: &Configuration) -> Result<String, ErrBox> { | ||
// todo :) | ||
Ok(text.to_string()) | ||
pub fn format_text(_file_path: &Path, text: &str, config: &Configuration) -> Result<String, ErrBox> { | ||
let node = parse_node(text)?; | ||
|
||
Ok(dprint_core::formatting::format( | ||
|| parse_items(&node, text, config), | ||
config_to_print_options(text, config), | ||
)) | ||
} | ||
|
||
#[cfg(feature = "tracing")] | ||
pub fn trace_file(_file_path: &Path, text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult { | ||
let node = parse_node(text).unwrap(); | ||
|
||
dprint_core::formatting::trace_printing(|| parse_items(node, text, config), config_to_print_options(text, config)) | ||
} | ||
|
||
fn parse_node(text: &str) -> Result<Dockerfile, ErrBox> { | ||
Ok(Dockerfile::parse(text)?) | ||
} | ||
|
||
fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions { | ||
PrintOptions { | ||
indent_width: 1, | ||
max_width: config.line_width, | ||
use_tabs: false, | ||
new_line_text: resolve_new_line_kind(text, config.new_line_kind), | ||
} | ||
} |
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,6 @@ | ||
pub mod configuration; | ||
mod format_text; | ||
mod parser; | ||
|
||
pub use format_text::format_text; | ||
|
||
|
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,22 @@ | ||
use std::collections::HashSet; | ||
|
||
use super::helpers::Node; | ||
use crate::configuration::Configuration; | ||
|
||
pub struct Context<'a> { | ||
pub config: &'a Configuration, | ||
pub text: &'a str, | ||
pub handled_comments: HashSet<usize>, | ||
pub current_node: Option<Node<'a>>, | ||
} | ||
|
||
impl<'a> Context<'a> { | ||
pub fn new(text: &'a str, config: &'a Configuration) -> Self { | ||
Self { | ||
config, | ||
text, | ||
handled_comments: HashSet::new(), | ||
current_node: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use dockerfile_parser::*; | ||
|
||
macro_rules! create_node_ref { | ||
($($variant_name:ident($node_name:ident),)*) => { | ||
#[derive(Clone, Copy)] | ||
pub enum Node<'a> { | ||
$( | ||
$variant_name(&'a $node_name), | ||
)* | ||
} | ||
|
||
$( | ||
impl<'a> From<&'a $node_name> for Node<'a> { | ||
fn from(instruction: &'a $node_name) -> Node<'a> { | ||
Node::$variant_name(instruction) | ||
} | ||
} | ||
)* | ||
} | ||
} | ||
|
||
create_node_ref!( | ||
Arg(ArgInstruction), | ||
Cmd(CmdInstruction), | ||
Copy(CopyInstruction), | ||
CopyFlag(CopyFlag), | ||
From(FromInstruction), | ||
Label(LabelInstruction), | ||
LabelLabel(Label), | ||
Run(RunInstruction), | ||
Entrypoint(EntrypointInstruction), | ||
Env(EnvInstruction), | ||
EnvVar(EnvVar), | ||
Misc(MiscInstruction), | ||
String(SpannedString), | ||
BreakableString(BreakableString), | ||
StringArray(StringArray), | ||
Comment(Comment), | ||
); | ||
|
||
impl<'a> Node<'a> { | ||
#[allow(dead_code)] | ||
pub fn span(&self) -> Span { | ||
use Node::*; | ||
match self { | ||
From(node) => node.span, | ||
Arg(node) => node.span, | ||
Label(node) => node.span, | ||
LabelLabel(node) => node.span, | ||
Run(node) => node.span, | ||
Entrypoint(node) => node.span, | ||
Cmd(node) => node.span, | ||
Copy(node) => node.span, | ||
CopyFlag(node) => node.span, | ||
Env(node) => node.span, | ||
EnvVar(node) => node.span, | ||
Misc(node) => node.span, | ||
String(node) => node.span, | ||
BreakableString(node) => node.span, | ||
StringArray(node) => node.span, | ||
Comment(node) => node.span, | ||
} | ||
} | ||
} | ||
|
||
pub struct Comment { | ||
pub span: Span, | ||
pub text: String, | ||
} | ||
|
||
pub fn parse_comments(text: &str, offset: usize) -> Vec<Comment> { | ||
let mut comments = Vec::new(); | ||
let mut char_iterator = text.char_indices(); | ||
let mut in_start_comment_context = true; | ||
|
||
while let Some((i, c)) = char_iterator.next() { | ||
// leading whitespace is supported but discouraged | ||
if in_start_comment_context && c.is_whitespace() { | ||
continue; | ||
} | ||
|
||
if in_start_comment_context && matches!(c, '#') { | ||
let start_index = i; | ||
let mut end_index = i; | ||
while let Some((i, c)) = char_iterator.next() { | ||
if c == '\n' { | ||
break; | ||
} | ||
end_index = i + c.len_utf8(); | ||
} | ||
comments.push(Comment { | ||
span: Span::new(offset + start_index, offset + end_index), | ||
text: text[start_index + 1..end_index].to_string(), | ||
}); | ||
in_start_comment_context = true; | ||
} else { | ||
in_start_comment_context = false; | ||
} | ||
} | ||
|
||
comments | ||
} | ||
|
||
impl<'a> From<&'a Instruction> for Node<'a> { | ||
fn from(instruction: &'a Instruction) -> Node<'a> { | ||
use Instruction::*; | ||
match instruction { | ||
From(node) => node.into(), | ||
Arg(node) => node.into(), | ||
Label(node) => node.into(), | ||
Run(node) => node.into(), | ||
Entrypoint(node) => node.into(), | ||
Cmd(node) => node.into(), | ||
Copy(node) => node.into(), | ||
Env(node) => node.into(), | ||
Misc(node) => node.into(), | ||
} | ||
} | ||
} |
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,5 @@ | ||
mod context; | ||
mod helpers; | ||
mod parse; | ||
|
||
pub use parse::*; |
Oops, something went wrong.