-
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
9 changed files
with
646 additions
and
641 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "dprint-plugin-dockerfile" | ||
version = "0.1.1" | ||
authors = ["David Sherret <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
homepage = "https://github.com/dprint/dprint-plugin-dockerfile" | ||
keywords = ["formatting", "formatter", "docker", "dockerfile"] | ||
license = "MIT" | ||
|
@@ -26,7 +26,7 @@ tracing = ["dprint-core/tracing"] | |
|
||
[dependencies] | ||
dockerfile-parser = { git = "https://github.com/dsherret/dockerfile-parser-rs", branch = "span-for-nodes" } | ||
dprint-core = { version = "0.46.4", features = ["formatting"] } | ||
dprint-core = { version = "0.47.0", 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
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,37 +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> { | ||
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), | ||
} | ||
} | ||
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> { | ||
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,71 +1,71 @@ | ||
use std::collections::HashSet; | ||
use std::rc::Rc; | ||
|
||
use dockerfile_parser::Dockerfile; | ||
use dockerfile_parser::Span; | ||
|
||
use super::helpers::parse_comments; | ||
use super::helpers::Node; | ||
use crate::configuration::Configuration; | ||
|
||
pub struct Context<'a> { | ||
pub config: &'a Configuration, | ||
pub dockerfile: &'a Dockerfile, | ||
pub text: &'a str, | ||
pub handled_comments: HashSet<usize>, | ||
current_node: Option<Node<'a>>, | ||
parent_stack: Vec<Node<'a>>, | ||
pub parse_string_content: bool, | ||
} | ||
|
||
impl<'a> Context<'a> { | ||
pub fn new(text: &'a str, dockerfile: &'a Dockerfile, config: &'a Configuration) -> Self { | ||
Self { | ||
config, | ||
text, | ||
dockerfile, | ||
handled_comments: HashSet::new(), | ||
current_node: None, | ||
parent_stack: Vec::new(), | ||
parse_string_content: false, | ||
} | ||
} | ||
|
||
pub fn span_text(&self, span: &Span) -> &str { | ||
&self.text[span.start..span.end] | ||
} | ||
|
||
pub fn set_current_node(&mut self, node: Node<'a>) { | ||
if let Some(parent) = self.current_node.take() { | ||
self.parent_stack.push(parent); | ||
} | ||
self.current_node = Some(node); | ||
} | ||
|
||
pub fn pop_current_node(&mut self) { | ||
self.current_node = self.parent_stack.pop(); | ||
} | ||
|
||
pub fn parent(&self) -> Option<&Node<'a>> { | ||
self.parent_stack.last() | ||
} | ||
|
||
pub fn parse_nodes_with_comments(&mut self, start_pos: usize, end_pos: usize, nodes: impl Iterator<Item = Node<'a>>) -> Vec<Node<'a>> { | ||
let mut result = Vec::new(); | ||
let mut last_pos = start_pos; | ||
for node in nodes { | ||
let text = &self.text[last_pos..node.span().start]; | ||
for comment in parse_comments(text, last_pos) { | ||
result.push(Node::CommentRc(Rc::new(comment))); | ||
} | ||
let node_end = node.span().end; | ||
result.push(node); | ||
last_pos = node_end; | ||
} | ||
let text = &self.text[last_pos..end_pos]; | ||
for comment in parse_comments(text, last_pos) { | ||
result.push(Node::CommentRc(Rc::new(comment))); | ||
} | ||
result | ||
} | ||
} | ||
use std::collections::HashSet; | ||
use std::rc::Rc; | ||
|
||
use dockerfile_parser::Dockerfile; | ||
use dockerfile_parser::Span; | ||
|
||
use super::helpers::parse_comments; | ||
use super::helpers::Node; | ||
use crate::configuration::Configuration; | ||
|
||
pub struct Context<'a> { | ||
pub config: &'a Configuration, | ||
pub dockerfile: &'a Dockerfile, | ||
pub text: &'a str, | ||
pub handled_comments: HashSet<usize>, | ||
current_node: Option<Node<'a>>, | ||
parent_stack: Vec<Node<'a>>, | ||
pub parse_string_content: bool, | ||
} | ||
|
||
impl<'a> Context<'a> { | ||
pub fn new(text: &'a str, dockerfile: &'a Dockerfile, config: &'a Configuration) -> Self { | ||
Self { | ||
config, | ||
text, | ||
dockerfile, | ||
handled_comments: HashSet::new(), | ||
current_node: None, | ||
parent_stack: Vec::new(), | ||
parse_string_content: false, | ||
} | ||
} | ||
|
||
pub fn span_text(&self, span: &Span) -> &str { | ||
&self.text[span.start..span.end] | ||
} | ||
|
||
pub fn set_current_node(&mut self, node: Node<'a>) { | ||
if let Some(parent) = self.current_node.take() { | ||
self.parent_stack.push(parent); | ||
} | ||
self.current_node = Some(node); | ||
} | ||
|
||
pub fn pop_current_node(&mut self) { | ||
self.current_node = self.parent_stack.pop(); | ||
} | ||
|
||
pub fn parent(&self) -> Option<&Node<'a>> { | ||
self.parent_stack.last() | ||
} | ||
|
||
pub fn parse_nodes_with_comments(&mut self, start_pos: usize, end_pos: usize, nodes: impl Iterator<Item = Node<'a>>) -> Vec<Node<'a>> { | ||
let mut result = Vec::new(); | ||
let mut last_pos = start_pos; | ||
for node in nodes { | ||
let text = &self.text[last_pos..node.span().start]; | ||
for comment in parse_comments(text, last_pos) { | ||
result.push(Node::CommentRc(Rc::new(comment))); | ||
} | ||
let node_end = node.span().end; | ||
result.push(node); | ||
last_pos = node_end; | ||
} | ||
let text = &self.text[last_pos..end_pos]; | ||
for comment in parse_comments(text, last_pos) { | ||
result.push(Node::CommentRc(Rc::new(comment))); | ||
} | ||
result | ||
} | ||
} |
Oops, something went wrong.