|
| 1 | +use crate::dsl::{SpacingDsl, SpacingRule, SpaceLoc, SpaceValue}; |
| 2 | +use crate::pattern::{Pattern, PatternSet}; |
| 3 | +use crate::rules::spacing; |
| 4 | +use crate::trav_util::{walk, walk_nodes, walk_tokens}; |
| 5 | + |
| 6 | +use ra_syntax::{ |
| 7 | + NodeOrToken, SmolStr, SyntaxElement, |
| 8 | + SyntaxKind::{self, *}, |
| 9 | + SyntaxNode, SyntaxToken, TextRange, TextUnit, WalkEvent, T, |
| 10 | +}; |
| 11 | + |
| 12 | +use std::collections::{HashMap, HashSet}; |
| 13 | + |
| 14 | +// TODO make more like intellij's fmt model |
| 15 | +// Model holds immutable tree and mutable intermediate model to produce diff |
| 16 | +// the model will probably have to create its own tree to add the extra |
| 17 | +// info to each token/node: |
| 18 | +// |
| 19 | +// [1,2,3]; |
| 20 | +// can be Brace token, ident, comma all of which knows their own rules and apply |
| 21 | +// them accordingly to produce [1, 2, 3]; ??? |
| 22 | + |
| 23 | +#[derive(Debug)] |
| 24 | +// this will probably be an enum that holds a struct for space/indent/ect |
| 25 | +struct AtomEdit { |
| 26 | + space_loc: SpaceLoc, |
| 27 | + val: SpaceValue, |
| 28 | + start: TextUnit, |
| 29 | +} |
| 30 | + |
| 31 | +impl AtomEdit {} |
| 32 | + |
| 33 | +#[derive(Debug)] |
| 34 | +pub(crate) struct SynBlock { |
| 35 | + //indent: some enum? |
| 36 | + kind: SyntaxKind, |
| 37 | + node: SyntaxNode, |
| 38 | + tokens: Vec<SyntaxToken>, |
| 39 | + range: TextRange, |
| 40 | + edits: Vec<AtomEdit>, |
| 41 | + ws_rules: Vec<SpacingRule>, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug)] |
| 45 | +pub(crate) struct EditTree { |
| 46 | + blocks: Vec<SynBlock>, |
| 47 | + flat_edit: Vec<SyntaxElement>, |
| 48 | + edits: Vec<AtomEdit>, |
| 49 | +} |
| 50 | + |
| 51 | +// each block will have knowledge of spacing and indent, |
| 52 | +// |
| 53 | +impl SynBlock { |
| 54 | + pub(crate) fn build_block( |
| 55 | + node: SyntaxNode, |
| 56 | + // this must change when indent comes back to generic? |
| 57 | + patt: &PatternSet<&'_ SpacingRule>, |
| 58 | + ) -> Self { |
| 59 | + // are there node rules? |
| 60 | + let mut ws_rules = patt.matching(NodeOrToken::Node(node.clone())) |
| 61 | + .cloned() |
| 62 | + .collect::<Vec<_>>(); |
| 63 | + |
| 64 | + let mut token_rules = walk_tokens(&node) |
| 65 | + .flat_map::<Vec<_>, _>(|t| patt.matching(NodeOrToken::Token(t)).collect()) |
| 66 | + // TODO get rid of clone? |
| 67 | + .cloned() |
| 68 | + .collect::<Vec<SpacingRule>>(); |
| 69 | + |
| 70 | + ws_rules.append(&mut token_rules); |
| 71 | + //println!("{:?}", ws_rules); |
| 72 | + Self { |
| 73 | + kind: node.kind(), |
| 74 | + node: node.clone(), |
| 75 | + tokens: walk_tokens(&node).collect(), |
| 76 | + range: node.text_range(), |
| 77 | + edits: vec![], |
| 78 | + ws_rules, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Default for EditTree { |
| 84 | + fn default() -> Self { |
| 85 | + Self { |
| 86 | + blocks: vec![], |
| 87 | + flat_edit: vec![], |
| 88 | + edits: vec![], |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl EditTree { |
| 94 | + pub(crate) fn from_root(root: &SyntaxNode, patt: PatternSet<&'_ SpacingRule>) -> Self { |
| 95 | + Self::default().build_tree(root, patt) |
| 96 | + } |
| 97 | + |
| 98 | + pub(crate) fn build_tree( |
| 99 | + mut self, |
| 100 | + root: &SyntaxNode, |
| 101 | + patt: PatternSet<&'_ SpacingRule>, |
| 102 | + ) -> Self { |
| 103 | + self.blocks = walk_nodes(root).map(|n| SynBlock::build_block(n, &patt)).collect(); |
| 104 | + self |
| 105 | + } |
| 106 | + |
| 107 | + pub(crate) fn to_string(&self) -> String { |
| 108 | + let ordered = walk_tokens(&self.blocks[0].node) |
| 109 | + .map(|t| t.to_string()) |
| 110 | + .collect::<String>(); |
| 111 | + //let set: HashSet<SyntaxElement> = HashSet::from_iter(ordered); |
| 112 | + println!("{:?}", ordered); |
| 113 | + ordered |
| 114 | + } |
| 115 | +} |
0 commit comments