aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'helix-core/src')
-rw-r--r--helix-core/src/history.rs30
-rw-r--r--helix-core/src/indent.rs1
-rw-r--r--helix-core/src/lib.rs1
-rw-r--r--helix-core/src/match_brackets.rs9
-rw-r--r--helix-core/src/movement.rs2
-rw-r--r--helix-core/src/numbers.rs499
-rw-r--r--helix-core/src/path.rs4
-rw-r--r--helix-core/src/selection.rs7
-rw-r--r--helix-core/src/syntax.rs23
-rw-r--r--helix-core/src/textobject.rs11
10 files changed, 572 insertions, 15 deletions
diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs
index b53c01fe..4b1c8d3b 100644
--- a/helix-core/src/history.rs
+++ b/helix-core/src/history.rs
@@ -1,4 +1,4 @@
-use crate::{ChangeSet, Rope, State, Transaction};
+use crate::{Assoc, ChangeSet, Range, Rope, State, Transaction};
use once_cell::sync::Lazy;
use regex::Regex;
use std::num::NonZeroUsize;
@@ -133,6 +133,32 @@ impl History {
Some(&self.revisions[last_child.get()].transaction)
}
+ // Get the position of last change
+ pub fn last_edit_pos(&self) -> Option<usize> {
+ if self.current == 0 {
+ return None;
+ }
+ let current_revision = &self.revisions[self.current];
+ let primary_selection = current_revision
+ .inversion
+ .selection()
+ .expect("inversion always contains a selection")
+ .primary();
+ let (_from, to, _fragment) = current_revision
+ .transaction
+ .changes_iter()
+ // find a change that matches the primary selection
+ .find(|(from, to, _fragment)| Range::new(*from, *to).overlaps(&primary_selection))
+ // or use the first change
+ .or_else(|| current_revision.transaction.changes_iter().next())
+ .unwrap();
+ let pos = current_revision
+ .transaction
+ .changes()
+ .map_pos(to, Assoc::After);
+ Some(pos)
+ }
+
fn lowest_common_ancestor(&self, mut a: usize, mut b: usize) -> usize {
use std::collections::HashSet;
let mut a_path_set = HashSet::new();
@@ -256,7 +282,7 @@ impl History {
}
/// Whether to undo by a number of edits or a duration of time.
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone, Copy)]
pub enum UndoKind {
Steps(usize),
TimePeriod(std::time::Duration),
diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs
index df158363..88ab09b5 100644
--- a/helix-core/src/indent.rs
+++ b/helix-core/src/indent.rs
@@ -450,6 +450,7 @@ where
language: vec![LanguageConfiguration {
scope: "source.rust".to_string(),
file_types: vec!["rs".to_string()],
+ shebangs: vec![],
language_id: "Rust".to_string(),
highlight_config: OnceCell::new(),
config: None,
diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs
index f4284139..7d790406 100644
--- a/helix-core/src/lib.rs
+++ b/helix-core/src/lib.rs
@@ -10,6 +10,7 @@ pub mod line_ending;
pub mod macros;
pub mod match_brackets;
pub mod movement;
+pub mod numbers;
pub mod object;
pub mod path;
mod position;
diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs
index a4b2fb9c..136ce320 100644
--- a/helix-core/src/match_brackets.rs
+++ b/helix-core/src/match_brackets.rs
@@ -1,6 +1,13 @@
use crate::{Rope, Syntax};
-const PAIRS: &[(char, char)] = &[('(', ')'), ('{', '}'), ('[', ']'), ('<', '>')];
+const PAIRS: &[(char, char)] = &[
+ ('(', ')'),
+ ('{', '}'),
+ ('[', ']'),
+ ('<', '>'),
+ ('\'', '\''),
+ ('"', '"'),
+];
// limit matching pairs to only ( ) { } [ ] < >
#[must_use]
diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs
index 9e85bd21..01a8f890 100644
--- a/helix-core/src/movement.rs
+++ b/helix-core/src/movement.rs
@@ -168,7 +168,7 @@ pub fn backwards_skip_while<F>(slice: RopeSlice, pos: usize, fun: F) -> Option<u
where
F: Fn(char) -> bool,
{
- let mut chars_starting_from_next = slice.chars_at(pos + 1);
+ let mut chars_starting_from_next = slice.chars_at(pos);
let mut backwards = iter::from_fn(|| chars_starting_from_next.prev()).enumerate();
backwards.find_map(|(i, c)| {
if !fun(c) {
diff --git a/helix-core/src/numbers.rs b/helix-core/src/numbers.rs
new file mode 100644
index 00000000..e9f3c898
--- /dev/null
+++ b/helix-core/src/numbers.rs
@@ -0,0 +1,499 @@
+use std::borrow::Cow;
+
+use ropey::RopeSlice;
+
+use crate::{
+ textobject::{textobject_word, TextObject},
+ Range, Tendril,
+};
+
+#[derive(Debug, PartialEq, Eq)]
+pub struct NumberIncrementor<'a> {
+ pub range: Range,
+ pub value: i64,
+ pub radix: u32,
+
+ text: RopeSlice<'a>,
+}
+
+impl<'a> NumberIncrementor<'a> {
+ /// Return information about number under rang if there is one.
+ pub fn from_range(text: RopeSlice, range: Range) -> Option<NumberIncrementor> {
+ // If the cursor is on the minus sign of a number we want to get the word textobject to the
+ // right of it.
+ let range = if range.to() < text.len_chars()
+ && range.to() - range.from() <= 1
+ && text.char(range.from()) == '-'
+ {
+ Range::new(range.from() + 1, range.to() + 1)
+ } else {
+ range
+ };
+
+ let range = textobject_word(text, range, TextObject::Inside, 1, false);
+
+ // If there is a minus sign to the left of the word object, we want to include it in the range.
+ let range = if range.from() > 0 && text.char(range.from() - 1) == '-' {
+ range.extend(range.from() - 1, range.from())
+ } else {
+ range
+ };
+
+ let word: String = text
+ .slice(range.from()..range.to())
+ .chars()
+ .filter(|&c| c != '_')
+ .collect();
+ let (radix, prefixed) = if word.starts_with("0x") {
+ (16, true)
+ } else if word.starts_with("0o") {
+ (8, true)
+ } else if word.starts_with("0b") {
+ (2, true)
+ } else {
+ (10, false)
+ };
+
+ let number = if prefixed { &word[2..] } else { &word };
+
+ let value = i128::from_str_radix(number, radix).ok()?;
+ if (value.is_positive() && value.leading_zeros() < 64)
+ || (value.is_negative() && value.leading_ones() < 64)
+ {
+ return None;
+ }
+
+ let value = value as i64;
+ Some(NumberIncrementor {
+ range,
+ value,
+ radix,
+ text,
+ })
+ }
+
+ /// Add `amount` to the number and return the formatted text.
+ pub fn incremented_text(&self, amount: i64) -> Tendril {
+ let old_text: Cow<str> = self.text.slice(self.range.from()..self.range.to()).into();
+ let old_length = old_text.len();
+ let new_value = self.value.wrapping_add(amount);
+
+ // Get separator indexes from right to left.
+ let separator_rtl_indexes: Vec<usize> = old_text
+ .chars()
+ .rev()
+ .enumerate()
+ .filter_map(|(i, c)| if c == '_' { Some(i) } else { None })
+ .collect();
+
+ let format_length = if self.radix == 10 {
+ match (self.value.is_negative(), new_value.is_negative()) {
+ (true, false) => old_length - 1,
+ (false, true) => old_length + 1,
+ _ => old_text.len(),
+ }
+ } else {
+ old_text.len() - 2
+ } - separator_rtl_indexes.len();
+
+ let mut new_text = match self.radix {
+ 2 => format!("0b{:01$b}", new_value, format_length),
+ 8 => format!("0o{:01$o}", new_value, format_length),
+ 10 if old_text.starts_with('0') || old_text.starts_with("-0") => {
+ format!("{:01$}", new_value, format_length)
+ }
+ 10 => format!("{}", new_value),
+ 16 => {
+ let (lower_count, upper_count): (usize, usize) =
+ old_text.chars().skip(2).fold((0, 0), |(lower, upper), c| {
+ (
+ lower + c.is_ascii_lowercase().then(|| 1).unwrap_or(0),
+ upper + c.is_ascii_uppercase().then(|| 1).unwrap_or(0),
+ )
+ });
+ if upper_count > lower_count {
+ format!("0x{:01$X}", new_value, format_length)
+ } else {
+ format!("0x{:01$x}", new_value, format_length)
+ }
+ }
+ _ => unimplemented!("radix not supported: {}", self.radix),
+ };
+
+ // Add separators from original number.
+ for &rtl_index in &separator_rtl_indexes {
+ if rtl_index < new_text.len() {
+ let new_index = new_text.len() - rtl_index;
+ new_text.insert(new_index, '_');
+ }
+ }
+
+ // Add in additional separators if necessary.
+ if new_text.len() > old_length && !separator_rtl_indexes.is_empty() {
+ let spacing = match separator_rtl_indexes.as_slice() {
+ [.., b, a] => a - b - 1,
+ _ => separator_rtl_indexes[0],
+ };
+
+ let prefix_length = if self.radix == 10 { 0 } else { 2 };
+ if let Some(mut index) = new_text.find('_') {
+ while index - prefix_length > spacing {
+ index -= spacing;
+ new_text.insert(index, '_');
+ }
+ }
+ }
+
+ new_text.into()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use crate::Rope;
+
+ #[test]
+ fn test_decimal_at_point() {
+ let rope = Rope::from_str("Test text 12345 more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 15),
+ value: 12345,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_uppercase_hexadecimal_at_point() {
+ let rope = Rope::from_str("Test text 0x123ABCDEF more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 21),
+ value: 0x123ABCDEF,
+ radix: 16,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_lowercase_hexadecimal_at_point() {
+ let rope = Rope::from_str("Test text 0xfa3b4e more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 18),
+ value: 0xfa3b4e,
+ radix: 16,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_octal_at_point() {
+ let rope = Rope::from_str("Test text 0o1074312 more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 19),
+ value: 0o1074312,
+ radix: 8,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_binary_at_point() {
+ let rope = Rope::from_str("Test text 0b10111010010101 more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 26),
+ value: 0b10111010010101,
+ radix: 2,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_negative_decimal_at_point() {
+ let rope = Rope::from_str("Test text -54321 more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 16),
+ value: -54321,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_decimal_with_leading_zeroes_at_point() {
+ let rope = Rope::from_str("Test text 000045326 more text.");
+ let range = Range::point(12);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 19),
+ value: 45326,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_negative_decimal_cursor_on_minus_sign() {
+ let rope = Rope::from_str("Test text -54321 more text.");
+ let range = Range::point(10);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(10, 16),
+ value: -54321,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_number_under_range_start_of_rope() {
+ let rope = Rope::from_str("100");
+ let range = Range::point(0);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(0, 3),
+ value: 100,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_number_under_range_end_of_rope() {
+ let rope = Rope::from_str("100");
+ let range = Range::point(2);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(0, 3),
+ value: 100,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_number_surrounded_by_punctuation() {
+ let rope = Rope::from_str(",100;");
+ let range = Range::point(1);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range),
+ Some(NumberIncrementor {
+ range: Range::new(1, 4),
+ value: 100,
+ radix: 10,
+ text: rope.slice(..),
+ })
+ );
+ }
+
+ #[test]
+ fn test_not_a_number_point() {
+ let rope = Rope::from_str("Test text 45326 more text.");
+ let range = Range::point(6);
+ assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
+ }
+
+ #[test]
+ fn test_number_too_large_at_point() {
+ let rope = Rope::from_str("Test text 0xFFFFFFFFFFFFFFFFF more text.");
+ let range = Range::point(12);
+ assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
+ }
+
+ #[test]
+ fn test_number_cursor_one_right_of_number() {
+ let rope = Rope::from_str("100 ");
+ let range = Range::point(3);
+ assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
+ }
+
+ #[test]
+ fn test_number_cursor_one_left_of_number() {
+ let rope = Rope::from_str(" 100");
+ let range = Range::point(0);
+ assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
+ }
+
+ #[test]
+ fn test_increment_basic_decimal_numbers() {
+ let tests = [
+ ("100", 1, "101"),
+ ("100", -1, "99"),
+ ("99", 1, "100"),
+ ("100", 1000, "1100"),
+ ("100", -1000, "-900"),
+ ("-1", 1, "0"),
+ ("-1", 2, "1"),
+ ("1", -1, "0"),
+ ("1", -2, "-1"),
+ ];
+
+ for (original, amount, expected) in tests {
+ let rope = Rope::from_str(original);
+ let range = Range::point(0);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range)
+ .unwrap()
+ .incremented_text(amount),
+ expected.into()
+ );
+ }
+ }
+
+ #[test]
+ fn test_increment_basic_hexadedimal_numbers() {
+ let tests = [
+ ("0x0100", 1, "0x0101"),
+ ("0x0100", -1, "0x00ff"),
+ ("0x0001", -1, "0x0000"),
+ ("0x0000", -1, "0xffffffffffffffff"),
+ ("0xffffffffffffffff", 1, "0x0000000000000000"),
+ ("0xffffffffffffffff", 2, "0x0000000000000001"),
+ ("0xffffffffffffffff", -1, "0xfffffffffffffffe"),
+ ("0xABCDEF1234567890", 1, "0xABCDEF1234567891"),
+ ("0xabcdef1234567890", 1, "0xabcdef1234567891"),
+ ];
+
+ for (original, amount, expected) in tests {
+ let rope = Rope::from_str(original);
+ let range = Range::point(0);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range)
+ .unwrap()
+ .incremented_text(amount),
+ expected.into()
+ );
+ }
+ }
+
+ #[test]
+ fn test_increment_basic_octal_numbers() {
+ let tests = [
+ ("0o0107", 1, "0o0110"),
+ ("0o0110", -1, "0o0107"),
+ ("0o0001", -1, "0o0000"),
+ ("0o7777", 1, "0o10000"),
+ ("0o1000", -1, "0o0777"),
+ ("0o0107", 10, "0o0121"),
+ ("0o0000", -1, "0o1777777777777777777777"),
+ ("0o1777777777777777777777", 1, "0o0000000000000000000000"),
+ ("0o1777777777777777777777", 2, "0o0000000000000000000001"),
+ ("0o1777777777777777777777", -1, "0o1777777777777777777776"),
+ ];
+
+ for (original, amount, expected) in tests {
+ let rope = Rope::from_str(original);
+ let range = Range::point(0);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range)
+ .unwrap()
+ .incremented_text(amount),
+ expected.into()
+ );
+ }
+ }
+
+ #[test]
+ fn test_increment_basic_binary_numbers() {
+ let tests = [
+ ("0b00000100", 1, "0b00000101"),
+ ("0b00000100", -1, "0b00000011"),
+ ("0b00000100", 2, "0b00000110"),
+ ("0b00000100", -2, "0b00000010"),
+ ("0b00000001", -1, "0b00000000"),
+ ("0b00111111", 10, "0b01001001"),
+ ("0b11111111", 1, "0b100000000"),
+ ("0b10000000", -1, "0b01111111"),
+ (
+ "0b0000",
+ -1,
+ "0b1111111111111111111111111111111111111111111111111111111111111111",
+ ),
+ (
+ "0b1111111111111111111111111111111111111111111111111111111111111111",
+ 1,
+ "0b0000000000000000000000000000000000000000000000000000000000000000",
+ ),
+ (
+ "0b1111111111111111111111111111111111111111111111111111111111111111",
+ 2,
+ "0b0000000000000000000000000000000000000000000000000000000000000001",
+ ),
+ (
+ "0b1111111111111111111111111111111111111111111111111111111111111111",
+ -1,
+ "0b1111111111111111111111111111111111111111111111111111111111111110",
+ ),
+ ];
+
+ for (original, amount, expected) in tests {
+ let rope = Rope::from_str(original);
+ let range = Range::point(0);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range)
+ .unwrap()
+ .incremented_text(amount),
+ expected.into()
+ );
+ }
+ }
+
+ #[test]
+ fn test_increment_with_separators() {
+ let tests = [
+ ("999_999", 1, "1_000_000"),
+ ("1_000_000", -1, "999_999"),
+ ("-999_999", -1, "-1_000_000"),
+ ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
+ ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
+ ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
+ ("0x0000_0000", -1, "0xffff_ffff_ffff_ffff"),
+ ("0x0000_0000_0000", -1, "0xffff_ffff_ffff_ffff"),
+ ("0b01111111_11111111", 1, "0b10000000_00000000"),
+ ("0b11111111_11111111", 1, "0b1_00000000_00000000"),
+ ];
+
+ for (original, amount, expected) in tests {
+ let rope = Rope::from_str(original);
+ let range = Range::point(0);
+ assert_eq!(
+ NumberIncrementor::from_range(rope.slice(..), range)
+ .unwrap()
+ .incremented_text(amount),
+ expected.into()
+ );
+ }
+ }
+}
diff --git a/helix-core/src/path.rs b/helix-core/src/path.rs
index 6c37cfa1..a6644465 100644
--- a/helix-core/src/path.rs
+++ b/helix-core/src/path.rs
@@ -40,7 +40,6 @@ pub fn expand_tilde(path: &Path) -> PathBuf {
/// needs to improve on.
/// Copied from cargo: <https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81>
pub fn get_normalized_path(path: &Path) -> PathBuf {
- let path = expand_tilde(path);
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
@@ -72,10 +71,11 @@ pub fn get_normalized_path(path: &Path) -> PathBuf {
/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
/// here if the path exists, just normalize it's components.
pub fn get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> {
+ let path = expand_tilde(path);
let path = if path.is_relative() {
std::env::current_dir().map(|current_dir| current_dir.join(path))?
} else {
- path.to_path_buf()
+ path
};
Ok(get_normalized_path(path.as_path()))
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index f3b5d2c8..b4d1dffa 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -360,7 +360,7 @@ impl Selection {
self.normalize()
}
- /// Adds a new range to the selection and makes it the primary range.
+ /// Removes a range from the selection.
pub fn remove(mut self, index: usize) -> Self {
assert!(
self.ranges.len() > 1,
@@ -528,14 +528,15 @@ impl<'a> IntoIterator for &'a Selection {
// TODO: checkSelection -> check if valid for doc length && sorted
-pub fn keep_matches(
+pub fn keep_or_remove_matches(
text: RopeSlice,
selection: &Selection,
regex: &crate::regex::Regex,
+ remove: bool,
) -> Option<Selection> {
let result: SmallVec<_> = selection
.iter()
- .filter(|range| regex.is_match(&range.fragment(text)))
+ .filter(|range| regex.is_match(&range.fragment(text)) ^ remove)
.copied()
.collect();
diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs
index 18504c21..f136ecd0 100644
--- a/helix-core/src/syntax.rs
+++ b/helix-core/src/syntax.rs
@@ -40,18 +40,21 @@ where
}
#[derive(Debug, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
pub struct Configuration {
pub language: Vec<LanguageConfiguration>,
}
// largely based on tree-sitter/cli/src/loader.rs
#[derive(Debug, Serialize, Deserialize)]
-#[serde(rename_all = "kebab-case")]
+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LanguageConfiguration {
#[serde(rename = "name")]
pub language_id: String,
pub scope: String, // source.rust
pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
+ #[serde(default)]
+ pub shebangs: Vec<String>, // interpreter(s) associated with language
pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
pub comment_token: Option<String>,
@@ -309,6 +312,7 @@ pub struct Loader {
// highlight_names ?
language_configs: Vec<Arc<LanguageConfiguration>>,
language_config_ids_by_file_type: HashMap<String, usize>, // Vec<usize>
+ language_config_ids_by_shebang: HashMap<String, usize>,
}
impl Loader {
@@ -316,6 +320,7 @@ impl Loader {
let mut loader = Self {
language_configs: Vec::new(),
language_config_ids_by_file_type: HashMap::new(),
+ language_config_ids_by_shebang: HashMap::new(),
};
for config in config.language {
@@ -328,6 +333,11 @@ impl Loader {
.language_config_ids_by_file_type
.insert(file_type.clone(), language_id);
}
+ for shebang in &config.shebangs {
+ loader
+ .language_config_ids_by_shebang
+ .insert(shebang.clone(), language_id);
+ }
loader.language_configs.push(Arc::new(config));
}
@@ -353,6 +363,17 @@ impl Loader {
// TODO: content_regex handling conflict resolution
}
+ pub fn language_config_for_shebang(&self, source: &Rope) -> Option<Arc<LanguageConfiguration>> {
+ let line = Cow::from(source.line(0));
+ static SHEBANG_REGEX: Lazy<Regex> =
+ Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
+ let configuration_id = SHEBANG_REGEX
+ .captures(&line)
+ .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
+
+ configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
+ }
+
pub fn language_config_for_scope(&self, scope: &str) -> Option<Arc<LanguageConfiguration>> {
self.language_configs
.iter()
diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs
index 975ed115..24f063d4 100644
--- a/helix-core/src/textobject.rs
+++ b/helix-core/src/textobject.rs
@@ -10,7 +10,7 @@ use crate::surround;
use crate::syntax::LanguageConfiguration;
use crate::Range;
-fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction) -> usize {
+fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize {
use CharCategory::{Eol, Whitespace};
let iter = match direction {
@@ -33,7 +33,7 @@ fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction) ->
match categorize_char(ch) {
Eol | Whitespace => return pos,
category => {
- if category != prev_category && pos != 0 && pos != slice.len_chars() {
+ if !long && category != prev_category && pos != 0 && pos != slice.len_chars() {
return pos;
} else {
match direction {
@@ -70,13 +70,14 @@ pub fn textobject_word(
range: Range,
textobject: TextObject,
_count: usize,
+ long: bool,
) -> Range {
let pos = range.cursor(slice);
- let word_start = find_word_boundary(slice, pos, Direction::Backward);
+ let word_start = find_word_boundary(slice, pos, Direction::Backward, long);
let word_end = match slice.get_char(pos).map(categorize_char) {
None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos,
- _ => find_word_boundary(slice, pos + 1, Direction::Forward),
+ _ => find_word_boundary(slice, pos + 1, Direction::Forward, long),
};
// Special case.
@@ -268,7 +269,7 @@ mod test {
let slice = doc.slice(..);
for &case in scenario {
let (pos, objtype, expected_range) = case;
- let result = textobject_word(slice, Range::point(pos), objtype, 1);
+ let result = textobject_word(slice, Range::point(pos), objtype, 1, false);
assert_eq!(
result,
expected_range.into(),