Skip to content

Commit cf3c84f

Browse files
committed
feat: offer to show duplicate base
Closes #50
1 parent 64e8cc4 commit cf3c84f

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/index.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use lasso::{Spur, ThreadedRodeo};
1414
use request::WorkDoneProgressCreate;
1515
use ropey::Rope;
1616
use smart_default::SmartDefault;
17-
use tower_lsp_server::lsp_types::request::{ShowDocument, ShowMessageRequest};
1817
use tower_lsp_server::{lsp_types::*, Client};
1918
use tracing::{debug, warn};
2019
use tree_sitter::QueryCursor;
@@ -23,7 +22,7 @@ use xmlparser::{Token, Tokenizer};
2322

2423
use crate::model::{Model, ModelIndex, ModelType};
2524
use crate::record::Record;
26-
use crate::utils::{path_contains, ts_range_to_lsp_range, ByteOffset, ByteRange, MinLoc, RangeExt};
25+
use crate::utils::{path_contains, ts_range_to_lsp_range, uri_from_file_path, ByteOffset, ByteRange, MinLoc, RangeExt};
2726
use crate::{errloc, format_loc, loc, ok, ImStr};
2827

2928
mod record;
@@ -210,9 +209,12 @@ impl Index {
210209
.or_default()
211210
.insert(module_key.into(), module_path.to_str().expect("non-utf8 path").into());
212211
if let Some(duplicate) = duplicate {
213-
warn!(old = %duplicate, new = ?module_path, "duplicate module {module_name}");
214-
if let Some(client) = client.clone() {
215-
outputs.spawn(Self::notify_duplicate_base(client));
212+
if let (Some(client), "base") = (client.clone(), module_name.as_str()) {
213+
tokio::spawn(Self::notify_duplicate_base(
214+
client,
215+
duplicate,
216+
module_path.to_path_buf(),
217+
));
216218
}
217219
}
218220
if tsconfig {
@@ -339,16 +341,16 @@ impl Index {
339341
elapsed: t0.elapsed(),
340342
}))
341343
}
342-
async fn notify_duplicate_base(client: Client) -> anyhow::Result<Output> {
344+
async fn notify_duplicate_base(client: Client, old_path: ImStr, new_path: PathBuf) -> anyhow::Result<Output> {
343345
let resp = client
344-
.send_request::<ShowMessageRequest>(ShowMessageRequestParams {
345-
typ: MessageType::WARNING,
346-
message: concat!(
346+
.show_message_request(
347+
MessageType::WARNING,
348+
concat!(
347349
"Duplicate base module found. The language server's performance may be affected.\n",
348350
"Please configure your workspace (or .odoo_lsp) to only include one version of Odoo as described in the wiki.",
349351
)
350352
.to_string(),
351-
actions: Some(vec![
353+
Some(vec![
352354
MessageActionItem {
353355
title: "Go to odoo-lsp wiki".to_string(),
354356
properties: Default::default(),
@@ -357,20 +359,42 @@ impl Index {
357359
title: "Dismiss".to_string(),
358360
properties: Default::default(),
359361
},
362+
MessageActionItem {
363+
title: "Show conflicting modules".to_string(),
364+
properties: Default::default(),
365+
},
360366
]),
361-
})
367+
)
362368
.await;
363369
match resp {
364370
Ok(Some(resp)) if resp.title == "Go to odoo-lsp wiki" => {
365371
_ = client
366-
.send_request::<ShowDocument>(ShowDocumentParams {
372+
.show_document(ShowDocumentParams {
367373
uri: Uri::from_str("https://github.com/Desdaemon/odoo-lsp/wiki#usage").unwrap(),
368374
external: Some(true),
369375
take_focus: None,
370376
selection: None,
371377
})
372378
.await;
373379
}
380+
Ok(Some(resp)) if resp.title == "Show conflicting modules" => {
381+
_ = client
382+
.show_document(ShowDocumentParams {
383+
uri: uri_from_file_path(Path::new(old_path.as_str())).unwrap(),
384+
external: Some(false),
385+
take_focus: Some(true),
386+
selection: None,
387+
})
388+
.await;
389+
_ = client
390+
.show_document(ShowDocumentParams {
391+
uri: uri_from_file_path(&new_path).unwrap(),
392+
external: Some(false),
393+
take_focus: Some(false),
394+
selection: None,
395+
})
396+
.await;
397+
}
374398
_ => {}
375399
}
376400

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ impl LanguageServer for Backend {
658658
.workspaces
659659
.iter()
660660
.map(|entry| {
661-
let scope_uri = from_file_path(entry.key());
661+
let scope_uri = uri_from_file_path(entry.key());
662662
ConfigurationItem {
663663
section: Some("odoo-lsp".into()),
664664
scope_uri,
@@ -884,7 +884,7 @@ impl LanguageServer for Backend {
884884
_ = self
885885
.client
886886
.show_document(ShowDocumentParams {
887-
uri: from_file_path(&location.path.to_path()).unwrap(),
887+
uri: uri_from_file_path(&location.path.to_path()).unwrap(),
888888
external: Some(false),
889889
take_focus: Some(true),
890890
selection: Some(location.range),

src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ impl UriExt for tower_lsp_server::lsp_types::Uri {
538538
}
539539
}
540540

541-
pub fn from_file_path(path: &Path) -> Option<Uri> {
541+
pub fn uri_from_file_path(path: &Path) -> Option<Uri> {
542542
let fragment = if !path.is_absolute() {
543543
Cow::from(strict_canonicalize(path).ok()?)
544544
} else {
@@ -635,7 +635,7 @@ mod tests {
635635

636636
use crate::utils::{strict_canonicalize, DisplayExt};
637637

638-
use super::{from_file_path, to_display_path, UriExt, WSL};
638+
use super::{to_display_path, uri_from_file_path, UriExt, WSL};
639639
use pretty_assertions::assert_eq;
640640
use tower_lsp_server::lsp_types::Uri;
641641

@@ -660,7 +660,7 @@ mod tests {
660660
#[test]
661661
fn test_path_roundtrip_conversion() {
662662
let src = strict_canonicalize(Path::new(".")).unwrap();
663-
let conv = from_file_path(&src).unwrap();
663+
let conv = uri_from_file_path(&src).unwrap();
664664
let roundtrip = conv.to_file_path().unwrap();
665665
assert_eq!(src, roundtrip, "conv={conv:?} conv_display={}", conv.display());
666666
}
@@ -672,7 +672,7 @@ mod tests {
672672
let path = uri.to_file_path().unwrap();
673673
assert_eq!(&path, Path::new("C:/Windows"), "uri={uri:?}");
674674

675-
let conv = from_file_path(&path).unwrap();
675+
let conv = uri_from_file_path(&path).unwrap();
676676

677677
assert_eq!(uri, conv, "path={path:?} left={} right={}", uri.as_str(), conv.as_str());
678678
}

0 commit comments

Comments
 (0)