2020-10-09 11:58:43 +04:00
|
|
|
use crate::{
|
2021-02-18 13:34:22 +04:00
|
|
|
find_first_non_whitespace_char,
|
2021-05-14 14:21:46 +04:00
|
|
|
syntax::{IndentQuery, LanguageConfiguration, Syntax},
|
2020-10-09 11:58:43 +04:00
|
|
|
tree_sitter::{Node, Tree},
|
2021-03-18 08:45:57 +04:00
|
|
|
Rope, RopeSlice,
|
2020-10-09 11:58:43 +04:00
|
|
|
};
|
|
|
|
|
2020-10-14 13:07:42 +04:00
|
|
|
/// To determine indentation of a newly inserted line, figure out the indentation at the last col
|
|
|
|
/// of the previous line.
|
|
|
|
|
2021-03-22 08:47:39 +04:00
|
|
|
fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
|
2020-10-09 11:58:43 +04:00
|
|
|
let mut len = 0;
|
|
|
|
for ch in line.chars() {
|
|
|
|
match ch {
|
2021-03-22 08:47:39 +04:00
|
|
|
'\t' => len += tab_width,
|
2020-10-09 11:58:43 +04:00
|
|
|
' ' => len += 1,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 08:47:39 +04:00
|
|
|
len / tab_width
|
2020-10-09 11:58:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the highest syntax node at position.
|
|
|
|
/// This is to identify the column where this node (e.g., an HTML closing tag) ends.
|
|
|
|
fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Node> {
|
2021-03-26 06:02:32 +04:00
|
|
|
let tree = syntax.tree();
|
2020-10-09 11:58:43 +04:00
|
|
|
|
2021-01-08 11:15:12 +04:00
|
|
|
// named_descendant
|
|
|
|
let mut node = match tree.root_node().descendant_for_byte_range(pos, pos) {
|
2020-10-09 11:58:43 +04:00
|
|
|
Some(node) => node,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
while let Some(parent) = node.parent() {
|
|
|
|
if parent.start_byte() == node.start_byte() {
|
|
|
|
node = parent
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
2021-05-14 14:21:46 +04:00
|
|
|
fn calculate_indentation(query: &IndentQuery, node: Option<Node>, newline: bool) -> usize {
|
|
|
|
// NOTE: can't use contains() on query because of comparing Vec<String> and &str
|
|
|
|
// https://doc.rust-lang.org/std/vec/struct.Vec.html#method.contains
|
2020-10-14 13:07:42 +04:00
|
|
|
|
2021-05-14 14:21:46 +04:00
|
|
|
let mut increment: i32 = 0;
|
2020-10-14 13:07:42 +04:00
|
|
|
|
2021-01-08 11:15:12 +04:00
|
|
|
let mut node = match node {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return 0,
|
|
|
|
};
|
2020-10-14 13:07:42 +04:00
|
|
|
|
2021-01-10 18:46:18 +04:00
|
|
|
let mut prev_start = node.start_position().row;
|
|
|
|
|
2021-01-08 11:15:12 +04:00
|
|
|
// if we're calculating indentation for a brand new line then the current node will become the
|
|
|
|
// parent node. We need to take it's indentation level into account too.
|
|
|
|
let node_kind = node.kind();
|
2021-05-14 14:21:46 +04:00
|
|
|
if newline && query.indent.contains(node_kind) {
|
2021-01-08 11:15:12 +04:00
|
|
|
increment += 1;
|
2020-10-09 11:58:43 +04:00
|
|
|
}
|
|
|
|
|
2021-01-08 11:15:12 +04:00
|
|
|
while let Some(parent) = node.parent() {
|
|
|
|
let parent_kind = parent.kind();
|
2021-01-10 18:46:18 +04:00
|
|
|
let start = parent.start_position().row;
|
2021-01-08 11:15:12 +04:00
|
|
|
|
2021-04-07 11:39:13 +04:00
|
|
|
// detect deeply nested indents in the same line
|
2021-04-14 09:28:31 +04:00
|
|
|
// .map(|a| { <-- ({ is two scopes
|
|
|
|
// let len = 1; <-- indents one level
|
|
|
|
// }) <-- }) is two scopes
|
2021-04-07 11:39:13 +04:00
|
|
|
let starts_same_line = start == prev_start;
|
|
|
|
|
2021-05-14 14:21:46 +04:00
|
|
|
if query.outdent.contains(node.kind()) && !starts_same_line {
|
2021-01-08 11:15:12 +04:00
|
|
|
// we outdent by skipping the rules for the current level and jumping up
|
2021-01-10 18:46:18 +04:00
|
|
|
// node = parent;
|
|
|
|
increment -= 1;
|
|
|
|
// continue;
|
2021-01-08 11:15:12 +04:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:21:46 +04:00
|
|
|
if query.indent.contains(parent_kind) // && not_first_or_last_sibling
|
2021-01-10 18:46:18 +04:00
|
|
|
&& !starts_same_line
|
|
|
|
{
|
2021-01-08 11:15:12 +04:00
|
|
|
// println!("is_scope {}", parent_kind);
|
2021-01-10 18:46:18 +04:00
|
|
|
prev_start = start;
|
2021-01-08 11:15:12 +04:00
|
|
|
increment += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// if last_scope && increment > 0 && ...{ ignore }
|
|
|
|
|
|
|
|
node = parent;
|
|
|
|
}
|
2020-10-14 13:07:42 +04:00
|
|
|
|
2021-01-10 18:46:18 +04:00
|
|
|
assert!(increment >= 0);
|
|
|
|
|
|
|
|
increment as usize
|
2020-10-09 11:58:43 +04:00
|
|
|
}
|
|
|
|
|
2021-03-22 08:47:39 +04:00
|
|
|
fn suggested_indent_for_line(
|
2021-05-14 14:21:46 +04:00
|
|
|
language_config: &LanguageConfiguration,
|
2021-03-22 08:47:39 +04:00
|
|
|
syntax: Option<&Syntax>,
|
|
|
|
text: RopeSlice,
|
|
|
|
line_num: usize,
|
|
|
|
tab_width: usize,
|
|
|
|
) -> usize {
|
2021-03-18 08:45:57 +04:00
|
|
|
let line = text.line(line_num);
|
2021-03-22 08:47:39 +04:00
|
|
|
let current = indent_level_for_line(line, tab_width);
|
2020-10-15 18:31:37 +04:00
|
|
|
|
2021-03-18 08:45:57 +04:00
|
|
|
if let Some(start) = find_first_non_whitespace_char(text, line_num) {
|
2021-05-14 14:21:46 +04:00
|
|
|
return suggested_indent_for_pos(Some(language_config), syntax, text, start, false);
|
2021-01-08 11:15:12 +04:00
|
|
|
};
|
2020-10-09 11:58:43 +04:00
|
|
|
|
2021-01-08 11:15:12 +04:00
|
|
|
// if the line is blank, indent should be zero
|
|
|
|
0
|
2020-10-14 13:07:42 +04:00
|
|
|
}
|
|
|
|
|
2021-01-08 11:15:12 +04:00
|
|
|
// TODO: two usecases: if we are triggering this for a new, blank line:
|
|
|
|
// - it should return 0 when mass indenting stuff
|
|
|
|
// - it should look up the wrapper node and count it too when we press o/O
|
|
|
|
pub fn suggested_indent_for_pos(
|
2021-05-14 14:21:46 +04:00
|
|
|
language_config: Option<&LanguageConfiguration>,
|
2021-01-08 11:15:12 +04:00
|
|
|
syntax: Option<&Syntax>,
|
2021-03-18 08:45:57 +04:00
|
|
|
text: RopeSlice,
|
2021-01-08 11:15:12 +04:00
|
|
|
pos: usize,
|
|
|
|
new_line: bool,
|
|
|
|
) -> usize {
|
2021-05-14 14:21:46 +04:00
|
|
|
if let (Some(query), Some(syntax)) = (
|
|
|
|
language_config.and_then(|config| config.indent_query()),
|
|
|
|
syntax,
|
|
|
|
) {
|
2021-03-18 08:45:57 +04:00
|
|
|
let byte_start = text.char_to_byte(pos);
|
2020-10-14 13:07:42 +04:00
|
|
|
let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
|
2020-10-09 11:58:43 +04:00
|
|
|
|
2021-05-14 14:21:46 +04:00
|
|
|
// let config = load indentation query config from Syntax(should contain language_config)
|
|
|
|
|
2021-01-08 11:31:19 +04:00
|
|
|
// TODO: special case for comments
|
|
|
|
// TODO: if preserve_leading_whitespace
|
2021-05-14 14:21:46 +04:00
|
|
|
calculate_indentation(query, node, new_line)
|
2020-10-09 11:58:43 +04:00
|
|
|
} else {
|
2020-10-15 18:31:37 +04:00
|
|
|
// TODO: heuristics for non-tree sitter grammars
|
2020-10-09 11:58:43 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-14 13:07:42 +04:00
|
|
|
fn test_indent_level() {
|
2021-03-22 08:47:39 +04:00
|
|
|
let tab_width = 4;
|
2020-10-09 11:58:43 +04:00
|
|
|
let line = Rope::from(" fn new"); // 8 spaces
|
2021-03-22 08:47:39 +04:00
|
|
|
assert_eq!(indent_level_for_line(line.slice(..), tab_width), 2);
|
2020-10-09 11:58:43 +04:00
|
|
|
let line = Rope::from("\t\t\tfn new"); // 3 tabs
|
2021-03-22 08:47:39 +04:00
|
|
|
assert_eq!(indent_level_for_line(line.slice(..), tab_width), 3);
|
2020-10-09 11:58:43 +04:00
|
|
|
// mixed indentation
|
|
|
|
let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
|
2021-03-22 08:47:39 +04:00
|
|
|
assert_eq!(indent_level_for_line(line.slice(..), tab_width), 3);
|
2020-10-09 11:58:43 +04:00
|
|
|
}
|
2020-10-14 13:07:42 +04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_suggested_indent_for_line() {
|
|
|
|
let doc = Rope::from(
|
2021-01-08 11:15:12 +04:00
|
|
|
"
|
2021-02-19 09:55:53 +04:00
|
|
|
use std::{
|
|
|
|
io::{self, stdout, Stdout, Write},
|
|
|
|
path::PathBuf,
|
|
|
|
sync::Arc,
|
|
|
|
time::Duration,
|
|
|
|
}
|
2021-01-08 11:15:12 +04:00
|
|
|
mod test {
|
2020-10-14 13:07:42 +04:00
|
|
|
fn hello_world() {
|
2021-01-08 11:15:12 +04:00
|
|
|
1 + 1;
|
|
|
|
|
|
|
|
let does_indentation_work = 1;
|
|
|
|
|
|
|
|
let test_function = function_with_param(this_param,
|
|
|
|
that_param
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_function = function_with_param(
|
|
|
|
this_param,
|
|
|
|
that_param
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_function = function_with_proper_indent(param1,
|
|
|
|
param2,
|
|
|
|
);
|
|
|
|
|
|
|
|
let selection = Selection::new(
|
|
|
|
changes
|
|
|
|
.clone()
|
|
|
|
.map(|(start, end, text): (usize, usize, Option<Tendril>)| {
|
|
|
|
let len = text.map(|text| text.len()).unwrap() - 1; // minus newline
|
|
|
|
let pos = start + len;
|
|
|
|
Range::new(pos, pos)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
2020-10-14 13:07:42 +04:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 11:15:12 +04:00
|
|
|
|
|
|
|
impl<A, D> MyTrait<A, D> for YourType
|
|
|
|
where
|
|
|
|
A: TraitB + TraitC,
|
|
|
|
D: TraitE + TraitF,
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
//
|
2021-01-10 18:46:18 +04:00
|
|
|
match test {
|
|
|
|
Some(a) => 1,
|
|
|
|
None => {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::panic::set_hook(Box::new(move |info| {
|
|
|
|
hook(info);
|
|
|
|
}));
|
|
|
|
|
|
|
|
{ { {
|
|
|
|
1
|
|
|
|
}}}
|
2021-01-08 11:15:12 +04:00
|
|
|
|
2021-03-18 08:45:57 +04:00
|
|
|
pub fn change<I>(document: &Document, changes: I) -> Self
|
2021-01-10 18:46:18 +04:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Change> + ExactSizeIterator,
|
|
|
|
{
|
2021-05-09 14:02:35 +04:00
|
|
|
[
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
];
|
|
|
|
(
|
|
|
|
1,
|
|
|
|
2
|
|
|
|
);
|
2021-01-10 18:46:18 +04:00
|
|
|
true
|
|
|
|
}
|
2020-10-14 13:07:42 +04:00
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2021-03-18 08:45:57 +04:00
|
|
|
let doc = Rope::from(doc);
|
2021-03-25 11:53:32 +04:00
|
|
|
use crate::syntax::{
|
|
|
|
Configuration, IndentationConfiguration, Lang, LanguageConfiguration, Loader,
|
|
|
|
};
|
|
|
|
use once_cell::sync::OnceCell;
|
2021-06-04 05:50:03 +04:00
|
|
|
let loader = Loader::new(
|
|
|
|
Configuration {
|
|
|
|
language: vec![LanguageConfiguration {
|
|
|
|
scope: "source.rust".to_string(),
|
|
|
|
file_types: vec!["rs".to_string()],
|
|
|
|
language_id: Lang::Rust,
|
|
|
|
highlight_config: OnceCell::new(),
|
|
|
|
//
|
|
|
|
roots: vec![],
|
|
|
|
language_server: None,
|
|
|
|
indent: Some(IndentationConfiguration {
|
|
|
|
tab_width: 4,
|
|
|
|
unit: String::from(" "),
|
|
|
|
}),
|
|
|
|
indent_query: OnceCell::new(),
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
Vec::new(),
|
|
|
|
);
|
2021-03-25 11:53:32 +04:00
|
|
|
|
2021-05-10 12:57:17 +04:00
|
|
|
// set runtime path so we can find the queries
|
|
|
|
let mut runtime = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
runtime.push("../runtime");
|
|
|
|
std::env::set_var("HELIX_RUNTIME", runtime.to_str().unwrap());
|
|
|
|
|
2021-03-25 11:53:32 +04:00
|
|
|
let language_config = loader.language_config_for_scope("source.rust").unwrap();
|
2021-02-24 11:07:39 +04:00
|
|
|
let highlight_config = language_config.highlight_config(&[]).unwrap();
|
2021-03-18 08:45:57 +04:00
|
|
|
let syntax = Syntax::new(&doc, highlight_config.clone());
|
|
|
|
let text = doc.slice(..);
|
2021-03-22 08:47:39 +04:00
|
|
|
let tab_width = 4;
|
2021-01-10 18:46:18 +04:00
|
|
|
|
2021-03-18 08:45:57 +04:00
|
|
|
for i in 0..doc.len_lines() {
|
2021-01-10 18:46:18 +04:00
|
|
|
let line = text.line(i);
|
2021-03-22 08:47:39 +04:00
|
|
|
let indent = indent_level_for_line(line, tab_width);
|
2021-01-10 18:46:18 +04:00
|
|
|
assert_eq!(
|
2021-05-14 14:21:46 +04:00
|
|
|
suggested_indent_for_line(&language_config, Some(&syntax), text, i, tab_width),
|
2021-01-10 18:46:18 +04:00
|
|
|
indent,
|
|
|
|
"line {}: {}",
|
|
|
|
i,
|
|
|
|
line
|
|
|
|
);
|
|
|
|
}
|
2020-10-14 13:07:42 +04:00
|
|
|
}
|
2020-10-09 11:58:43 +04:00
|
|
|
}
|