Skip to content

Commit

Permalink
Merge indent updates
Browse files Browse the repository at this point in the history
Extract `ancestors()` iterator

Extract indentation config into own struct

Use `usize` everywhere to avoid conversions

Propagate indentation config

Simplify indent edit

Respect indentation config in text edit

Move `indent()` method out of `Backend`

Add utils for testing text edits

Don't depend on document's Dashmap ref

Add indentation tests

Take outermost node on the line before analysis

Fix anchor matcher

Check for OOB line

Fix selection of node at point

Update indentation config from formatting options

Log format-on-type method

Fix indentation of trailing whitespace

Decline to indent if version does not match

Fix indentation of top-level expressions

Ignore formatting options if tab size differs

Apply suggestions from code review

Co-authored-by: Davis Vaughan <davis@rstudio.com>

Remove custom formatting request
  • Loading branch information
lionel- committed May 30, 2024
1 parent 79f003a commit 2be0a28
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 90 deletions.
43 changes: 27 additions & 16 deletions crates/ark/src/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ use crate::lsp;
/// Configuration of a document.
///
/// The naming follows <https://editorconfig.org/> where possible.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct DocumentConfig {
pub indent: IndentationConfig,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IndentationConfig {
/// Whether to insert spaces of tabs for one level of indentation.
pub indent_style: IndentStyle,

/// The number of spaces for one level of indentation.
pub indent_size: u8,
pub indent_size: usize,

/// The width of a tab. There may be projects with an `indent_size` of 4 and
/// a `tab_width` of 8 (e.g. GNU R).
pub tab_width: u8,
pub tab_width: usize,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -32,17 +37,17 @@ pub(crate) struct VscDocumentConfig {
// DEV NOTE: Update `section_from_key()` method after adding a field
pub insert_spaces: bool,
pub indent_size: VscIndentSize,
pub tab_size: u8,
pub tab_size: usize,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub(crate) enum VscIndentSize {
Alias(String),
Size(u8),
Size(usize),
}

impl Default for DocumentConfig {
impl Default for IndentationConfig {
fn default() -> Self {
Self {
indent_style: IndentStyle::Space,
Expand All @@ -63,15 +68,11 @@ impl VscDocumentConfig {
}
}

// Convert from VS Code representation of a document config to our own
// representation
/// Convert from VS Code representation of a document config to our own
/// representation. Currently one-to-one.
impl From<VscDocumentConfig> for DocumentConfig {
fn from(x: VscDocumentConfig) -> Self {
let indent_style = if x.insert_spaces {
IndentStyle::Space
} else {
IndentStyle::Tab
};
let indent_style = indent_style_from_lsp(x.insert_spaces);

let indent_size = match x.indent_size {
VscIndentSize::Size(size) => size,
Expand All @@ -86,9 +87,19 @@ impl From<VscDocumentConfig> for DocumentConfig {
};

Self {
indent_style,
indent_size,
tab_width: x.tab_size,
indent: IndentationConfig {
indent_style,
indent_size,
tab_width: x.tab_size,
},
}
}
}

pub(crate) fn indent_style_from_lsp(insert_spaces: bool) -> IndentStyle {
if insert_spaces {
IndentStyle::Space
} else {
IndentStyle::Tab
}
}
12 changes: 2 additions & 10 deletions crates/ark/src/lsp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::lsp::help_topic::help_topic;
use crate::lsp::help_topic::HelpTopicParams;
use crate::lsp::help_topic::HelpTopicResponse;
use crate::lsp::hover::r_hover;
use crate::lsp::indent::indent;
use crate::lsp::indent::indent_edit;
use crate::lsp::main_loop::LspState;
use crate::lsp::references::find_references;
use crate::lsp::selection_range::convert_selection_range_from_tree_sitter_to_lsp;
Expand All @@ -56,7 +56,6 @@ use crate::lsp::statement_range::statement_range;
use crate::lsp::statement_range::StatementRangeParams;
use crate::lsp::statement_range::StatementRangeResponse;
use crate::lsp::symbols;
use crate::lsp::traits::node::NodeExt;
use crate::r_task;

// Handlers that do not mutate the world state. They take a sharing reference or
Expand Down Expand Up @@ -332,12 +331,5 @@ pub(crate) fn handle_indent(
let pos = ctxt.position;
let point = convert_position_to_point(&doc.contents, pos);

// Find node at point
let node = doc
.ast
.root_node()
.find_closest_node_to_point(point)
.unwrap();

indent(pos, node, doc)
indent_edit(doc, point.row)
}
Loading

0 comments on commit 2be0a28

Please sign in to comment.