on master help sign in
lsp: Provide workspace root on client.initialize()
commit8d6fad4cac65083a0b08e49391c579ec793bc8f3 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 7 Jun 2021, 21:32 (5 years ago) parent14830e7
3 files changed+29−28
helix-core/src/lib.rs+24 helix-lsp/src/client.rs+4−3 helix-term/src/commands.rs+1−25
helix-core/src/lib.rs+24
@@ -45,6 +45,30 @@ pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) -
4545 None
4646}
4747
48+pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
49+ let current_dir = std::env::current_dir().expect("unable to determine current directory");
50+
51+ let root = match root {
52+ Some(root) => {
53+ let root = std::path::Path::new(root);
54+ if root.is_absolute() {
55+ root.to_path_buf()
56+ } else {
57+ current_dir.join(root)
58+ }
59+ }
60+ None => current_dir,
61+ };
62+
63+ for ancestor in root.ancestors() {
64+ // TODO: also use defined roots if git isn't found
65+ if ancestor.join(".git").is_dir() {
66+ return Some(ancestor.to_path_buf());
67+ }
68+ }
69+ None
70+}
71+
4872#[cfg(not(embed_runtime))]
4973pub fn runtime_dir() -> std::path::PathBuf {
5074 // runtime env var || dir where binary is located
helix-lsp/src/client.rs+4−3
@@ -3,7 +3,7 @@ use crate::{
33 Call, Error, OffsetEncoding, Result,
44};
55
6−use helix_core::{ChangeSet, Rope};
6+use helix_core::{find_root, ChangeSet, Rope};
77
88// use std::collections::HashMap;
99use std::future::Future;
@@ -218,13 +218,14 @@ impl Client {
218218
219219 pub async fn initialize(&mut self) -> Result<()> {
220220 // TODO: delay any requests that are triggered prior to initialize
221+ let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok());
221222
222223 #[allow(deprecated)]
223224 let params = lsp::InitializeParams {
224225 process_id: Some(std::process::id()),
226+ // root_path is obsolete, use root_uri
225227 root_path: None,
226− // root_uri: Some(lsp_types::Url::parse("file://localhost/")?),
227− root_uri: None, // set to project root in the future
228+ root_uri: root,
228229 initialization_options: None,
229230 capabilities: lsp::ClientCapabilities {
230231 text_document: Some(lsp::TextDocumentClientCapabilities {
helix-term/src/commands.rs+1−25
@@ -1,5 +1,5 @@
11use helix_core::{
2− comment, coords_at_pos, graphemes, indent, match_brackets,
2+ comment, coords_at_pos, find_root, graphemes, indent, match_brackets,
33 movement::{self, Direction},
44 object, pos_at_coords,
55 regex::{self, Regex},
@@ -1093,30 +1093,6 @@ pub fn command_mode(cx: &mut Context) {
10931093 cx.push_layer(Box::new(prompt));
10941094}
10951095
1096−fn find_root(root: Option<&str>) -> Option<PathBuf> {
1097− let current_dir = std::env::current_dir().expect("unable to determine current directory");
1098−
1099− let root = match root {
1100− Some(root) => {
1101− let root = Path::new(root);
1102− if root.is_absolute() {
1103− root.to_path_buf()
1104− } else {
1105− current_dir.join(root)
1106− }
1107− }
1108− None => current_dir,
1109− };
1110−
1111− for ancestor in root.ancestors() {
1112− // TODO: also use defined roots if git isn't found
1113− if ancestor.join(".git").is_dir() {
1114− return Some(ancestor.to_path_buf());
1115− }
1116− }
1117− None
1118−}
1119−
11201096pub fn file_picker(cx: &mut Context) {
11211097 let root = find_root(None).unwrap_or_else(|| PathBuf::from("./"));
11221098 let picker = ui::file_picker(root);