on master help sign in
lsp: Provide workspace root on client.initialize()
8d6fad4Blaž Hrastnik
helix-core/src/lib.rs+24
@@ -45,6 +45,30 @@ pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) -
45 None
45 None
46}
46}
47
47
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+
48#[cfg(not(embed_runtime))]
72#[cfg(not(embed_runtime))]
49pub fn runtime_dir() -> std::path::PathBuf {
73pub fn runtime_dir() -> std::path::PathBuf {
50 // runtime env var || dir where binary is located
74 // runtime env var || dir where binary is located
helix-lsp/src/client.rs+4−3
@@ -3,7 +3,7 @@ use crate::{
3 Call, Error, OffsetEncoding, Result,
3 Call, Error, OffsetEncoding, Result,
4};
4};
5
5
6−use helix_core::{ChangeSet, Rope};
6+use helix_core::{find_root, ChangeSet, Rope};
7
7
8// use std::collections::HashMap;
8// use std::collections::HashMap;
9use std::future::Future;
9use std::future::Future;
@@ -218,13 +218,14 @@ impl Client {
218
218
219 pub async fn initialize(&mut self) -> Result<()> {
219 pub async fn initialize(&mut self) -> Result<()> {
220 // TODO: delay any requests that are triggered prior to initialize
220 // 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());
221
222
222 #[allow(deprecated)]
223 #[allow(deprecated)]
223 let params = lsp::InitializeParams {
224 let params = lsp::InitializeParams {
224 process_id: Some(std::process::id()),
225 process_id: Some(std::process::id()),
226+ // root_path is obsolete, use root_uri
225 root_path: None,
227 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,
228 initialization_options: None,
229 initialization_options: None,
229 capabilities: lsp::ClientCapabilities {
230 capabilities: lsp::ClientCapabilities {
230 text_document: Some(lsp::TextDocumentClientCapabilities {
231 text_document: Some(lsp::TextDocumentClientCapabilities {
helix-term/src/commands.rs+1−25
@@ -1,5 +1,5 @@
1use helix_core::{
1use helix_core::{
2− comment, coords_at_pos, graphemes, indent, match_brackets,
2+ comment, coords_at_pos, find_root, graphemes, indent, match_brackets,
3 movement::{self, Direction},
3 movement::{self, Direction},
4 object, pos_at_coords,
4 object, pos_at_coords,
5 regex::{self, Regex},
5 regex::{self, Regex},
@@ -1093,30 +1093,6 @@ pub fn command_mode(cx: &mut Context) {
1093 cx.push_layer(Box::new(prompt));
1093 cx.push_layer(Box::new(prompt));
1094}
1094}
1095
1095
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−
1120pub fn file_picker(cx: &mut Context) {
1096pub fn file_picker(cx: &mut Context) {
1121 let root = find_root(None).unwrap_or_else(|| PathBuf::from("./"));
1097 let root = find_root(None).unwrap_or_else(|| PathBuf::from("./"));
1122 let picker = ui::file_picker(root);
1098 let picker = ui::file_picker(root);