Merge branch 'master' into great_line_ending_and_cursor_range_cleanup

pull/376/head
Nathan Vegdahl 3 years ago
commit e462f32723

@ -38,18 +38,18 @@ fn find_line_comment(
} }
#[must_use] #[must_use]
pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction { pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction {
let text = doc.slice(..); let text = doc.slice(..);
let mut changes: Vec<Change> = Vec::new(); let mut changes: Vec<Change> = Vec::new();
let token = "//"; let token = token.unwrap_or("//");
let comment = Tendril::from(format!("{} ", token)); let comment = Tendril::from(format!("{} ", token));
for selection in selection { for selection in selection {
let start = text.char_to_line(selection.from()); let start = text.char_to_line(selection.from());
let end = text.char_to_line(selection.to()); let end = text.char_to_line(selection.to());
let lines = start..end + 1; let lines = start..end + 1;
let (commented, skipped, min) = find_line_comment(token, text, lines.clone()); let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
changes.reserve((end - start).saturating_sub(skipped.len())); changes.reserve((end - start).saturating_sub(skipped.len()));
@ -95,14 +95,14 @@ mod test {
assert_eq!(res, (false, vec![1], 2)); assert_eq!(res, (false, vec![1], 2));
// comment // comment
let transaction = toggle_line_comments(&state.doc, &state.selection); let transaction = toggle_line_comments(&state.doc, &state.selection, None);
transaction.apply(&mut state.doc); transaction.apply(&mut state.doc);
state.selection = state.selection.clone().map(transaction.changes()); state.selection = state.selection.clone().map(transaction.changes());
assert_eq!(state.doc, " // 1\n\n // 2\n // 3"); assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
// uncomment // uncomment
let transaction = toggle_line_comments(&state.doc, &state.selection); let transaction = toggle_line_comments(&state.doc, &state.selection, None);
transaction.apply(&mut state.doc); transaction.apply(&mut state.doc);
state.selection = state.selection.clone().map(transaction.changes()); state.selection = state.selection.clone().map(transaction.changes());
assert_eq!(state.doc, " 1\n\n 2\n 3"); assert_eq!(state.doc, " 1\n\n 2\n 3");

@ -1,6 +1,4 @@
use ropey::Rope; use crate::{Rope, Transaction};
use crate::{Change, Transaction};
/// Compares `old` and `new` to generate a [`Transaction`] describing /// Compares `old` and `new` to generate a [`Transaction`] describing
/// the steps required to get from `old` to `new`. /// the steps required to get from `old` to `new`.
@ -25,34 +23,34 @@ pub fn compare_ropes(old: &Rope, new: &Rope) -> Transaction {
// The current position of the change needs to be tracked to // The current position of the change needs to be tracked to
// construct the `Change`s. // construct the `Change`s.
let mut pos = 0; let mut pos = 0;
let changes: Vec<Change> = diff Transaction::change(
.ops() old,
.iter() diff.ops()
.map(|op| op.as_tag_tuple()) .iter()
.filter_map(|(tag, old_range, new_range)| { .map(|op| op.as_tag_tuple())
// `old_pos..pos` is equivalent to `start..end` for where .filter_map(|(tag, old_range, new_range)| {
// the change should be applied. // `old_pos..pos` is equivalent to `start..end` for where
let old_pos = pos; // the change should be applied.
pos += old_range.end - old_range.start; let old_pos = pos;
pos += old_range.end - old_range.start;
match tag { match tag {
// Semantically, inserts and replacements are the same thing. // Semantically, inserts and replacements are the same thing.
similar::DiffTag::Insert | similar::DiffTag::Replace => { similar::DiffTag::Insert | similar::DiffTag::Replace => {
// This is the text from the `new` rope that should be // This is the text from the `new` rope that should be
// inserted into `old`. // inserted into `old`.
let text: &str = { let text: &str = {
let start = new.char_to_byte(new_range.start); let start = new.char_to_byte(new_range.start);
let end = new.char_to_byte(new_range.end); let end = new.char_to_byte(new_range.end);
&new_converted[start..end] &new_converted[start..end]
}; };
Some((old_pos, pos, Some(text.into()))) Some((old_pos, pos, Some(text.into())))
}
similar::DiffTag::Delete => Some((old_pos, pos, None)),
similar::DiffTag::Equal => None,
} }
similar::DiffTag::Delete => Some((old_pos, pos, None)), }),
similar::DiffTag::Equal => None, )
}
})
.collect();
Transaction::change(old, changes.into_iter())
} }
#[cfg(test)] #[cfg(test)]

@ -262,8 +262,10 @@ where
file_types: vec!["rs".to_string()], file_types: vec!["rs".to_string()],
language_id: "Rust".to_string(), language_id: "Rust".to_string(),
highlight_config: OnceCell::new(), highlight_config: OnceCell::new(),
config: None,
// //
roots: vec![], roots: vec![],
comment_token: None,
auto_format: false, auto_format: false,
language_server: None, language_server: None,
indent: Some(IndentationConfiguration { indent: Some(IndentationConfiguration {

@ -35,6 +35,8 @@ pub struct LanguageConfiguration {
pub scope: String, // source.rust pub scope: String, // source.rust
pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc> pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml> pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
pub comment_token: Option<String>,
pub config: Option<String>,
#[serde(default)] #[serde(default)]
pub auto_format: bool, pub auto_format: bool,

@ -473,11 +473,13 @@ impl Transaction {
/// Generate a transaction from a set of changes. /// Generate a transaction from a set of changes.
pub fn change<I>(doc: &Rope, changes: I) -> Self pub fn change<I>(doc: &Rope, changes: I) -> Self
where where
I: IntoIterator<Item = Change> + ExactSizeIterator, I: IntoIterator<Item = Change> + Iterator,
{ {
let len = doc.len_chars(); let len = doc.len_chars();
let mut changeset = ChangeSet::with_capacity(2 * changes.len() + 1); // rough estimate let (lower, upper) = changes.size_hint();
let size = upper.unwrap_or(lower);
let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
// TODO: verify ranges are ordered and not overlapping or change will panic. // TODO: verify ranges are ordered and not overlapping or change will panic.

@ -24,12 +24,14 @@ pub struct Client {
request_counter: AtomicU64, request_counter: AtomicU64,
capabilities: Option<lsp::ServerCapabilities>, capabilities: Option<lsp::ServerCapabilities>,
offset_encoding: OffsetEncoding, offset_encoding: OffsetEncoding,
config: Option<Value>,
} }
impl Client { impl Client {
pub fn start( pub fn start(
cmd: &str, cmd: &str,
args: &[String], args: &[String],
config: Option<Value>,
id: usize, id: usize,
) -> Result<(Self, UnboundedReceiver<(usize, Call)>)> { ) -> Result<(Self, UnboundedReceiver<(usize, Call)>)> {
let process = Command::new(cmd) let process = Command::new(cmd)
@ -57,6 +59,7 @@ impl Client {
request_counter: AtomicU64::new(0), request_counter: AtomicU64::new(0),
capabilities: None, capabilities: None,
offset_encoding: OffsetEncoding::Utf8, offset_encoding: OffsetEncoding::Utf8,
config,
}; };
// TODO: async client.initialize() // TODO: async client.initialize()
@ -214,13 +217,17 @@ impl Client {
// TODO: delay any requests that are triggered prior to initialize // TODO: delay any requests that are triggered prior to initialize
let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok()); let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok());
if self.config.is_some() {
log::info!("Using custom LSP config: {}", self.config.as_ref().unwrap());
}
#[allow(deprecated)] #[allow(deprecated)]
let params = lsp::InitializeParams { let params = lsp::InitializeParams {
process_id: Some(std::process::id()), process_id: Some(std::process::id()),
// root_path is obsolete, use root_uri // root_path is obsolete, use root_uri
root_path: None, root_path: None,
root_uri: root, root_uri: root,
initialization_options: None, initialization_options: self.config.clone(),
capabilities: lsp::ClientCapabilities { capabilities: lsp::ClientCapabilities {
text_document: Some(lsp::TextDocumentClientCapabilities { text_document: Some(lsp::TextDocumentClientCapabilities {
completion: Some(lsp::CompletionClientCapabilities { completion: Some(lsp::CompletionClientCapabilities {

@ -312,7 +312,12 @@ impl Registry {
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
// initialize a new client // initialize a new client
let id = self.counter.fetch_add(1, Ordering::Relaxed); let id = self.counter.fetch_add(1, Ordering::Relaxed);
let (mut client, incoming) = Client::start(&config.command, &config.args, id)?; let (mut client, incoming) = Client::start(
&config.command,
&config.args,
serde_json::from_str(language_config.config.as_deref().unwrap_or("")).ok(),
id,
)?;
// TODO: run this async without blocking // TODO: run this async without blocking
futures_executor::block_on(client.initialize())?; futures_executor::block_on(client.initialize())?;
s_incoming.push(UnboundedReceiverStream::new(incoming)); s_incoming.push(UnboundedReceiverStream::new(incoming));

@ -3499,7 +3499,11 @@ fn hover(cx: &mut Context) {
// comments // comments
fn toggle_comments(cx: &mut Context) { fn toggle_comments(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id)); let token = doc
.language_config()
.and_then(|lc| lc.comment_token.as_ref())
.map(|tc| tc.as_ref());
let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id); doc.append_changes_to_history(view.id);

@ -8,6 +8,7 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher; use fuzzy_matcher::FuzzyMatcher;
use helix_view::{graphics::Rect, Editor}; use helix_view::{graphics::Rect, Editor};
use tui::layout::Constraint;
pub trait Item { pub trait Item {
// TODO: sort_text // TODO: sort_text
@ -26,6 +27,8 @@ pub struct Menu<T: Item> {
/// (index, score) /// (index, score)
matches: Vec<(usize, i64)>, matches: Vec<(usize, i64)>,
widths: Vec<Constraint>,
callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>, callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
scroll: usize, scroll: usize,
@ -44,6 +47,7 @@ impl<T: Item> Menu<T> {
matcher: Box::new(Matcher::default()), matcher: Box::new(Matcher::default()),
matches: Vec::new(), matches: Vec::new(),
cursor: None, cursor: None,
widths: Vec::new(),
callback_fn: Box::new(callback_fn), callback_fn: Box::new(callback_fn),
scroll: 0, scroll: 0,
size: (0, 0), size: (0, 0),
@ -218,8 +222,33 @@ impl<T: Item + 'static> Component for Menu<T> {
EventResult::Ignored EventResult::Ignored
} }
// TODO: completion sorting
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
let width = std::cmp::min(30, viewport.0); let n = self
.options
.first()
.map(|option| option.row().cells.len())
.unwrap_or_default();
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
let row = option.row();
// maintain max for each column
for (i, cell) in row.cells.iter().enumerate() {
let width = cell.content.width();
if width > acc[i] {
acc[i] = width;
}
}
acc
});
let len = (max_lens.iter().sum::<usize>()) + n + 1; // +1: reserve some space for scrollbar
let width = len.min(viewport.0 as usize);
self.widths = max_lens
.into_iter()
.map(|len| Constraint::Length(len as u16))
.collect();
const MAX: usize = 10; const MAX: usize = 10;
let height = std::cmp::min(self.options.len(), MAX); let height = std::cmp::min(self.options.len(), MAX);
@ -263,13 +292,12 @@ impl<T: Item + 'static> Component for Menu<T> {
let scroll_line = (win_height - scroll_height) * scroll let scroll_line = (win_height - scroll_height) * scroll
/ std::cmp::max(1, len.saturating_sub(win_height)); / std::cmp::max(1, len.saturating_sub(win_height));
use tui::layout::Constraint;
let rows = options.iter().map(|option| option.row()); let rows = options.iter().map(|option| option.row());
let table = Table::new(rows) let table = Table::new(rows)
.style(style) .style(style)
.highlight_style(selected) .highlight_style(selected)
.column_spacing(1) .column_spacing(1)
.widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]); .widths(&self.widths);
use tui::widgets::TableState; use tui::widgets::TableState;

@ -36,7 +36,7 @@ use std::collections::HashMap;
/// capabilities of [`Text`]. /// capabilities of [`Text`].
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default)]
pub struct Cell<'a> { pub struct Cell<'a> {
content: Text<'a>, pub content: Text<'a>,
style: Style, style: Style,
} }
@ -81,7 +81,7 @@ where
/// By default, a row has a height of 1 but you can change this using [`Row::height`]. /// By default, a row has a height of 1 but you can change this using [`Row::height`].
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default)]
pub struct Row<'a> { pub struct Row<'a> {
cells: Vec<Cell<'a>>, pub cells: Vec<Cell<'a>>,
height: u16, height: u16,
style: Style, style: Style,
bottom_margin: u16, bottom_margin: u16,

@ -5,6 +5,17 @@ injection-regex = "rust"
file-types = ["rs"] file-types = ["rs"]
roots = [] roots = []
auto-format = true auto-format = true
comment_token = "//"
config = """
{
"cargo": {
"loadOutDirsFromCheck": true
},
"procMacro": {
"enable": false
}
}
"""
language-server = { command = "rust-analyzer" } language-server = { command = "rust-analyzer" }
indent = { tab-width = 4, unit = " " } indent = { tab-width = 4, unit = " " }
@ -15,6 +26,7 @@ scope = "source.toml"
injection-regex = "toml" injection-regex = "toml"
file-types = ["toml"] file-types = ["toml"]
roots = [] roots = []
comment_token = "#"
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -42,6 +54,7 @@ scope = "source.c"
injection-regex = "c" injection-regex = "c"
file-types = ["c"] # TODO: ["h"] file-types = ["c"] # TODO: ["h"]
roots = [] roots = []
comment_token = "//"
language-server = { command = "clangd" } language-server = { command = "clangd" }
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -52,6 +65,7 @@ scope = "source.cpp"
injection-regex = "cpp" injection-regex = "cpp"
file-types = ["cc", "cpp", "hpp", "h"] file-types = ["cc", "cpp", "hpp", "h"]
roots = [] roots = []
comment_token = "//"
language-server = { command = "clangd" } language-server = { command = "clangd" }
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -63,6 +77,7 @@ injection-regex = "go"
file-types = ["go"] file-types = ["go"]
roots = ["Gopkg.toml", "go.mod"] roots = ["Gopkg.toml", "go.mod"]
auto-format = true auto-format = true
comment_token = "//"
language-server = { command = "gopls" } language-server = { command = "gopls" }
# TODO: gopls needs utf-8 offsets? # TODO: gopls needs utf-8 offsets?
@ -74,6 +89,7 @@ scope = "source.js"
injection-regex = "^(js|javascript)$" injection-regex = "^(js|javascript)$"
file-types = ["js"] file-types = ["js"]
roots = [] roots = []
comment_token = "//"
# TODO: highlights-jsx, highlights-params # TODO: highlights-jsx, highlights-params
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -113,6 +129,7 @@ scope = "source.python"
injection-regex = "python" injection-regex = "python"
file-types = ["py"] file-types = ["py"]
roots = [] roots = []
comment_token = "#"
language-server = { command = "pyls" } language-server = { command = "pyls" }
# TODO: pyls needs utf-8 offsets # TODO: pyls needs utf-8 offsets
@ -133,6 +150,7 @@ scope = "source.ruby"
injection-regex = "ruby" injection-regex = "ruby"
file-types = ["rb"] file-types = ["rb"]
roots = [] roots = []
comment_token = "#"
language-server = { command = "solargraph", args = ["stdio"] } language-server = { command = "solargraph", args = ["stdio"] }
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -143,6 +161,7 @@ scope = "source.bash"
injection-regex = "bash" injection-regex = "bash"
file-types = ["sh", "bash"] file-types = ["sh", "bash"]
roots = [] roots = []
comment_token = "#"
language-server = { command = "bash-language-server", args = ["start"] } language-server = { command = "bash-language-server", args = ["start"] }
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -162,6 +181,7 @@ scope = "source.tex"
injection-regex = "tex" injection-regex = "tex"
file-types = ["tex"] file-types = ["tex"]
roots = [] roots = []
comment_token = "%"
indent = { tab-width = 4, unit = "\t" } indent = { tab-width = 4, unit = "\t" }
@ -171,6 +191,7 @@ scope = "source.julia"
injection-regex = "julia" injection-regex = "julia"
file-types = ["jl"] file-types = ["jl"]
roots = [] roots = []
comment_token = "#"
language-server = { command = "julia", args = [ "--startup-file=no", "--history-file=no", "-e", "using LanguageServer;using Pkg;import StaticLint;import SymbolServer;env_path = dirname(Pkg.Types.Context().env.project_file);server = LanguageServer.LanguageServerInstance(stdin, stdout, env_path, \"\");server.runlinter = true;run(server);" ] } language-server = { command = "julia", args = [ "--startup-file=no", "--history-file=no", "-e", "using LanguageServer;using Pkg;import StaticLint;import SymbolServer;env_path = dirname(Pkg.Types.Context().env.project_file);server = LanguageServer.LanguageServerInstance(stdin, stdout, env_path, \"\");server.runlinter = true;run(server);" ] }
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
@ -180,5 +201,6 @@ indent = { tab-width = 2, unit = " " }
# injection-regex = "haskell" # injection-regex = "haskell"
# file-types = ["hs"] # file-types = ["hs"]
# roots = [] # roots = []
# comment_token = "--"
# #
# indent = { tab-width = 2, unit = " " } # indent = { tab-width = 2, unit = " " }

Loading…
Cancel
Save