on master help sign in
Import some lsp utilities.
ab1d110Blaž Hrastnik
helix-term/src/commands.rs+25−39
@@ -7,27 +7,31 @@ use helix_core::{
7 SmallVec, Tendril, Transaction,
7 SmallVec, Tendril, Transaction,
8};
8};
9
9
10−use once_cell::sync::Lazy;
10+use helix_view::{
11+ document::Mode,
12+ view::{View, PADDING},
13+ Document, DocumentId, Editor, ViewId,
14+};
15+
16+use helix_lsp::{
17+ lsp,
18+ util::{lsp_pos_to_pos, pos_to_lsp_pos, range_to_lsp_range},
19+};
11
20
12use crate::{
21use crate::{
13 compositor::{Callback, Component, Compositor},
22 compositor::{Callback, Component, Compositor},
14 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
23 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
15};
24};
16
25
26+use crate::application::{LspCallbackWrapper, LspCallbacks};
27+use futures_util::FutureExt;
28+use std::future::Future;
29+
17use std::borrow::Cow;
30use std::borrow::Cow;
18use std::path::{Path, PathBuf};
31use std::path::{Path, PathBuf};
19
32
20−use helix_view::{
21− document::Mode,
22− view::{View, PADDING},
23− Document, DocumentId, Editor, ViewId,
24−};
25−
26use crossterm::event::{KeyCode, KeyEvent};
33use crossterm::event::{KeyCode, KeyEvent};
27−
34+use once_cell::sync::Lazy;
28−use helix_lsp::lsp;
29−
30−use crate::application::{LspCallbackWrapper, LspCallbacks};
31
35
32pub struct Context<'a> {
36pub struct Context<'a> {
33 pub count: usize,
37 pub count: usize,
@@ -40,9 +44,6 @@ pub struct Context<'a> {
40 pub status_msg: Option<String>,
44 pub status_msg: Option<String>,
41}
45}
42
46
43−use futures_util::FutureExt;
44−use std::future::Future;
45−
46impl<'a> Context<'a> {
47impl<'a> Context<'a> {
47 #[inline]
48 #[inline]
48 pub fn view(&mut self) -> &mut View {
49 pub fn view(&mut self) -> &mut View {
@@ -778,7 +779,6 @@ pub fn append_mode(cx: &mut Context) {
778
779
779const COMMAND_LIST: &[&str] = &["write", "open", "quit"];
780const COMMAND_LIST: &[&str] = &["write", "open", "quit"];
780
781
781−// TODO: I, A, o and O can share a lot of the primitives.
782pub fn command_mode(cx: &mut Context) {
782pub fn command_mode(cx: &mut Context) {
783 let prompt = Prompt::new(
783 let prompt = Prompt::new(
784 ":".to_owned(),
784 ":".to_owned(),
@@ -1091,7 +1091,7 @@ fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) {
1091 .expect("editor.open failed");
1091 .expect("editor.open failed");
1092 let (view, doc) = editor.current();
1092 let (view, doc) = editor.current();
1093 let definition_pos = location.range.start;
1093 let definition_pos = location.range.start;
1094− let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
1094+ let new_pos = lsp_pos_to_pos(doc.text(), definition_pos);
1095 doc.set_selection(view.id, Selection::point(new_pos));
1095 doc.set_selection(view.id, Selection::point(new_pos));
1096 }
1096 }
1097
1097
@@ -1122,8 +1122,7 @@ pub fn goto_definition(cx: &mut Context) {
1122 None => return,
1122 None => return,
1123 };
1123 };
1124
1124
1125− // TODO: blocking here is not ideal
1126− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1125+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1127
1126
1128 // TODO: handle fails
1127 // TODO: handle fails
1129 let res =
1128 let res =
@@ -1138,8 +1137,7 @@ pub fn goto_type_definition(cx: &mut Context) {
1138 None => return,
1137 None => return,
1139 };
1138 };
1140
1139
1141− // TODO: blocking here is not ideal
1142− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1140+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1143
1141
1144 // TODO: handle fails
1142 // TODO: handle fails
1145 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
1143 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
@@ -1154,8 +1152,7 @@ pub fn goto_implementation(cx: &mut Context) {
1154 None => return,
1152 None => return,
1155 };
1153 };
1156
1154
1157− // TODO: blocking here is not ideal
1158− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1155+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1159
1156
1160 // TODO: handle fails
1157 // TODO: handle fails
1161 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
1158 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
@@ -1170,8 +1167,7 @@ pub fn goto_reference(cx: &mut Context) {
1170 None => return,
1167 None => return,
1171 };
1168 };
1172
1169
1173− // TODO: blocking here is not ideal
1174− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1170+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1175
1171
1176 // TODO: handle fails
1172 // TODO: handle fails
1177 let res =
1173 let res =
@@ -1187,8 +1183,7 @@ pub fn signature_help(cx: &mut Context) {
1187 None => return,
1183 None => return,
1188 };
1184 };
1189
1185
1190− // TODO: blocking here is not ideal
1191− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1186+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1192
1187
1193 // TODO: handle fails
1188 // TODO: handle fails
1194
1189
@@ -1519,18 +1514,15 @@ pub fn unindent(cx: &mut Context) {
1519}
1514}
1520
1515
1521pub fn format_selections(cx: &mut Context) {
1516pub fn format_selections(cx: &mut Context) {
1522− use helix_lsp::lsp;
1523 let (view, doc) = cx.current();
1517 let (view, doc) = cx.current();
1524
1518
1525 // via lsp if available
1519 // via lsp if available
1526 // else via tree-sitter indentation calculations
1520 // else via tree-sitter indentation calculations
1527
1521
1528− // TODO: blocking here is not ideal
1529−
1530 let ranges: Vec<lsp::Range> = doc
1522 let ranges: Vec<lsp::Range> = doc
1531 .selection(view.id)
1523 .selection(view.id)
1532 .iter()
1524 .iter()
1533− .map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
1525+ .map(|range| range_to_lsp_range(doc.text(), *range))
1534 .collect();
1526 .collect();
1535
1527
1536 for range in ranges {
1528 for range in ranges {
@@ -1675,11 +1667,9 @@ pub fn completion(cx: &mut Context) {
1675 None => return,
1667 None => return,
1676 };
1668 };
1677
1669
1678− // TODO: blocking here is not ideal
1679− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1670+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1680
1671
1681 // TODO: handle fails
1672 // TODO: handle fails
1682−
1683 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap();
1673 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap();
1684
1674
1685 let trigger_offset = doc.selection(view.id).cursor();
1675 let trigger_offset = doc.selection(view.id).cursor();
@@ -1702,13 +1692,11 @@ pub fn completion(cx: &mut Context) {
1702 // TODO: if no completion, show some message or something
1692 // TODO: if no completion, show some message or something
1703 if !items.is_empty() {
1693 if !items.is_empty() {
1704 let completion = Completion::new(items, trigger_offset);
1694 let completion = Completion::new(items, trigger_offset);
1705−
1706− // Server error: content modified
1707−
1708 compositor.push(Box::new(completion));
1695 compositor.push(Box::new(completion));
1709 }
1696 }
1710 },
1697 },
1711 );
1698 );
1699+ // TODO: Server error: content modified
1712
1700
1713 // // TODO!: when iterating over items, show the docs in popup
1701 // // TODO!: when iterating over items, show the docs in popup
1714
1702
@@ -1717,8 +1705,6 @@ pub fn completion(cx: &mut Context) {
1717}
1705}
1718
1706
1719pub fn hover(cx: &mut Context) {
1707pub fn hover(cx: &mut Context) {
1720− use helix_lsp::lsp;
1721−
1722 let (view, doc) = cx.current();
1708 let (view, doc) = cx.current();
1723
1709
1724 let language_server = match doc.language_server() {
1710 let language_server = match doc.language_server() {
@@ -1730,7 +1716,7 @@ pub fn hover(cx: &mut Context) {
1730
1716
1731 // TODO: blocking here is not ideal, make commands async fn?
1717 // TODO: blocking here is not ideal, make commands async fn?
1732 // not like we can process additional input meanwhile though
1718 // not like we can process additional input meanwhile though
1733− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1719+ let pos = pos_to_lsp_pos(doc.text(), doc.selection(view.id).cursor());
1734
1720
1735 // TODO: handle fails
1721 // TODO: handle fails
1736 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1722 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))