Skip to content

Commit 93bc009

Browse files
Remove the state
1 parent 74c3bba commit 93bc009

File tree

7 files changed

+37
-107
lines changed

7 files changed

+37
-107
lines changed

crates/completion/src/item.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::fmt;
44

55
use hir::{Documentation, ModPath, Mutability};
66
use ide_db::helpers::{
7-
insert_use::{self, ImportScope, ImportScopePtr, MergeBehaviour},
7+
insert_use::{self, ImportScope, MergeBehaviour},
88
mod_path_to_ast,
99
};
10-
use syntax::{algo, SyntaxNode, TextRange};
10+
use syntax::{algo, TextRange};
1111
use text_edit::TextEdit;
1212

1313
use crate::config::SnippetCap;
@@ -275,32 +275,8 @@ pub struct ImportEdit {
275275
pub merge_behaviour: Option<MergeBehaviour>,
276276
}
277277

278-
#[derive(Debug, Clone)]
279-
pub struct ImportEditPtr {
280-
pub import_path: ModPath,
281-
pub import_scope: ImportScopePtr,
282-
pub merge_behaviour: Option<MergeBehaviour>,
283-
}
284-
285-
impl ImportEditPtr {
286-
pub fn into_import_edit(self, root: &SyntaxNode) -> Option<ImportEdit> {
287-
Some(ImportEdit {
288-
import_path: self.import_path,
289-
import_scope: self.import_scope.into_scope(root)?,
290-
merge_behaviour: self.merge_behaviour,
291-
})
292-
}
293-
}
294-
295278
impl ImportEdit {
296-
pub fn get_edit_ptr(&self) -> ImportEditPtr {
297-
ImportEditPtr {
298-
import_path: self.import_path.clone(),
299-
import_scope: self.import_scope.get_ptr(),
300-
merge_behaviour: self.merge_behaviour,
301-
}
302-
}
303-
279+
// TODO kb remove this at all now, since it's used only once?
304280
/// Attempts to insert the import to the given scope, producing a text edit.
305281
/// May return no edit in edge cases, such as scope already containing the import.
306282
pub fn to_text_edit(&self) -> Option<TextEdit> {

crates/completion/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ use crate::{completions::Completions, context::CompletionContext, item::Completi
1818

1919
pub use crate::{
2020
config::{CompletionConfig, CompletionResolveCapability},
21-
item::{
22-
CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, ImportEditPtr,
23-
InsertTextFormat,
24-
},
21+
item::{CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, InsertTextFormat},
2522
};
2623

2724
//FIXME: split the following feature into fine-grained features.

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub use crate::{
8181
};
8282
pub use completion::{
8383
CompletionConfig, CompletionItem, CompletionItemKind, CompletionResolveCapability,
84-
CompletionScore, ImportEdit, ImportEditPtr, InsertTextFormat,
84+
CompletionScore, ImportEdit, InsertTextFormat,
8585
};
8686
pub use ide_db::{
8787
call_info::CallInfo,

crates/ide_db/src/helpers/insert_use.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syntax::{
1111
edit::{AstNodeEdit, IndentLevel},
1212
make, AstNode, PathSegmentKind, VisibilityOwner,
1313
},
14-
AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken,
14+
AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
1515
};
1616
use test_utils::mark;
1717

@@ -21,36 +21,6 @@ pub enum ImportScope {
2121
Module(ast::ItemList),
2222
}
2323

24-
impl ImportScope {
25-
pub fn get_ptr(&self) -> ImportScopePtr {
26-
match self {
27-
ImportScope::File(file) => ImportScopePtr::File(SyntaxNodePtr::new(file.syntax())),
28-
ImportScope::Module(module) => {
29-
ImportScopePtr::Module(SyntaxNodePtr::new(module.syntax()))
30-
}
31-
}
32-
}
33-
}
34-
35-
#[derive(Debug, Clone)]
36-
pub enum ImportScopePtr {
37-
File(SyntaxNodePtr),
38-
Module(SyntaxNodePtr),
39-
}
40-
41-
impl ImportScopePtr {
42-
pub fn into_scope(self, root: &SyntaxNode) -> Option<ImportScope> {
43-
Some(match self {
44-
ImportScopePtr::File(file_ptr) => {
45-
ImportScope::File(ast::SourceFile::cast(file_ptr.to_node(root))?)
46-
}
47-
ImportScopePtr::Module(module_ptr) => {
48-
ImportScope::File(ast::SourceFile::cast(module_ptr.to_node(root))?)
49-
}
50-
})
51-
}
52-
}
53-
5424
impl ImportScope {
5525
pub fn from(syntax: SyntaxNode) -> Option<Self> {
5626
if let Some(module) = ast::Module::cast(syntax.clone()) {

crates/rust-analyzer/src/global_state.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{sync::Arc, time::Instant};
77

88
use crossbeam_channel::{unbounded, Receiver, Sender};
99
use flycheck::FlycheckHandle;
10-
use ide::{Analysis, AnalysisHost, Change, FileId, ImportEditPtr};
10+
use ide::{Analysis, AnalysisHost, Change, FileId};
1111
use ide_db::base_db::{CrateId, VfsPath};
1212
use lsp_types::{SemanticTokens, Url};
1313
use parking_lot::{Mutex, RwLock};
@@ -69,7 +69,6 @@ pub(crate) struct GlobalState {
6969
pub(crate) config: Config,
7070
pub(crate) analysis_host: AnalysisHost,
7171
pub(crate) diagnostics: DiagnosticCollection,
72-
pub(crate) completion_resolve_data: Arc<FxHashMap<usize, ImportEditPtr>>,
7372
pub(crate) mem_docs: FxHashMap<VfsPath, DocumentData>,
7473
pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
7574
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
@@ -91,7 +90,6 @@ pub(crate) struct GlobalStateSnapshot {
9190
pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
9291
vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
9392
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
94-
pub(crate) completion_resolve_data: Arc<FxHashMap<usize, ImportEditPtr>>,
9593
}
9694

9795
impl GlobalState {
@@ -123,7 +121,6 @@ impl GlobalState {
123121
config,
124122
analysis_host,
125123
diagnostics: Default::default(),
126-
completion_resolve_data: Arc::new(FxHashMap::default()),
127124
mem_docs: FxHashMap::default(),
128125
semantic_tokens_cache: Arc::new(Default::default()),
129126
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))),
@@ -194,7 +191,6 @@ impl GlobalState {
194191
check_fixes: Arc::clone(&self.diagnostics.check_fixes),
195192
mem_docs: self.mem_docs.clone(),
196193
semantic_tokens_cache: Arc::clone(&self.semantic_tokens_cache),
197-
completion_resolve_data: Arc::clone(&self.completion_resolve_data),
198194
}
199195
}
200196

crates/rust-analyzer/src/handlers.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use std::{
66
io::Write as _,
77
process::{self, Stdio},
8-
sync::Arc,
98
};
109

1110
use ide::{
@@ -26,7 +25,6 @@ use lsp_types::{
2625
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
2726
};
2827
use project_model::TargetKind;
29-
use rustc_hash::FxHashMap;
3028
use serde::{Deserialize, Serialize};
3129
use serde_json::to_value;
3230
use stdx::{format_to, split_once};
@@ -539,11 +537,10 @@ pub(crate) fn handle_runnables(
539537
}
540538

541539
pub(crate) fn handle_completion(
542-
global_state: &mut GlobalState,
540+
snap: GlobalStateSnapshot,
543541
params: lsp_types::CompletionParams,
544542
) -> Result<Option<lsp_types::CompletionResponse>> {
545543
let _p = profile::span("handle_completion");
546-
let snap = global_state.snapshot();
547544
let text_document_url = params.text_document_position.text_document.uri.clone();
548545
let position = from_proto::file_position(&snap, params.text_document_position)?;
549546
let completion_triggered_after_single_colon = {
@@ -574,7 +571,6 @@ pub(crate) fn handle_completion(
574571
};
575572
let line_index = snap.analysis.file_line_index(position.file_id)?;
576573
let line_endings = snap.file_line_endings(position.file_id);
577-
let mut completion_resolve_data = FxHashMap::default();
578574

579575
let items: Vec<CompletionItem> = items
580576
.into_iter()
@@ -584,16 +580,15 @@ pub(crate) fn handle_completion(
584580
to_proto::completion_item(&line_index, line_endings, item.clone());
585581

586582
if snap.config.completion.resolve_additional_edits_lazily() {
583+
// TODO kb add resolve data somehow here
587584
if let Some(import_edit) = item.import_to_add() {
588-
completion_resolve_data.insert(item_index, import_edit.get_edit_ptr());
589-
590-
let data = serde_json::to_value(&CompletionData {
591-
document_url: text_document_url.clone(),
592-
import_id: item_index,
593-
})
594-
.expect(&format!("Should be able to serialize usize value {}", item_index));
585+
// let data = serde_json::to_value(&CompletionData {
586+
// document_url: text_document_url.clone(),
587+
// import_id: item_index,
588+
// })
589+
// .expect(&format!("Should be able to serialize usize value {}", item_index));
595590
for new_item in &mut new_completion_items {
596-
new_item.data = Some(data.clone());
591+
// new_item.data = Some(data.clone());
597592
}
598593
}
599594
}
@@ -602,8 +597,6 @@ pub(crate) fn handle_completion(
602597
})
603598
.collect();
604599

605-
global_state.completion_resolve_data = Arc::new(completion_resolve_data);
606-
607600
let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
608601
Ok(Some(completion_list.into()))
609602
}
@@ -624,33 +617,31 @@ pub(crate) fn handle_completion_resolve(
624617
return Ok(original_completion);
625618
}
626619

627-
let (import_edit_ptr, document_url) = match original_completion
620+
let resolve_data = match original_completion
628621
.data
629-
.as_ref()
630-
.map(|data| serde_json::from_value::<CompletionData>(data.clone()))
622+
.take()
623+
.map(|data| serde_json::from_value::<CompletionResolveData>(data))
631624
.transpose()?
632-
.and_then(|data| {
633-
let import_edit_ptr = snap.completion_resolve_data.get(&data.import_id).cloned();
634-
Some((import_edit_ptr, data.document_url))
635-
}) {
625+
{
636626
Some(data) => data,
637627
None => return Ok(original_completion),
638628
};
639629

640-
let file_id = from_proto::file_id(&snap, &document_url)?;
641-
let root = snap.analysis.parse(file_id)?;
642-
643-
if let Some(import_to_add) =
644-
import_edit_ptr.and_then(|import_edit| import_edit.into_import_edit(root.syntax()))
645-
{
646-
// FIXME actually add all additional edits here? see `to_proto::completion_item` for more
647-
append_import_edits(
648-
&mut original_completion,
649-
&import_to_add,
650-
snap.analysis.file_line_index(file_id)?.as_ref(),
651-
snap.file_line_endings(file_id),
652-
);
653-
}
630+
// TODO kb get the resolve data and somehow reparse the whole ast again?
631+
// let file_id = from_proto::file_id(&snap, &document_url)?;
632+
// let root = snap.analysis.parse(file_id)?;
633+
634+
// if let Some(import_to_add) =
635+
// import_edit_ptr.and_then(|import_edit| import_edit.into_import_edit(root.syntax()))
636+
// {
637+
// // FIXME actually add all additional edits here? see `to_proto::completion_item` for more
638+
// append_import_edits(
639+
// &mut original_completion,
640+
// &import_to_add,
641+
// snap.analysis.file_line_index(file_id)?.as_ref(),
642+
// snap.file_line_endings(file_id),
643+
// );
644+
// }
654645

655646
Ok(original_completion)
656647
}
@@ -1614,7 +1605,7 @@ fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>)
16141605
}
16151606

16161607
#[derive(Debug, Serialize, Deserialize)]
1617-
struct CompletionData {
1608+
struct CompletionResolveData {
16181609
document_url: Url,
16191610
import_id: usize,
16201611
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,6 @@ impl GlobalState {
436436
handlers::handle_matching_brace(s.snapshot(), p)
437437
})?
438438
.on_sync::<lsp_ext::MemoryUsage>(|s, p| handlers::handle_memory_usage(s, p))?
439-
.on_sync::<lsp_types::request::Completion>(handlers::handle_completion)?
440-
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
441439
.on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)
442440
.on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)
443441
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
@@ -455,6 +453,8 @@ impl GlobalState {
455453
.on::<lsp_types::request::GotoDefinition>(handlers::handle_goto_definition)
456454
.on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
457455
.on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
456+
.on::<lsp_types::request::Completion>(handlers::handle_completion)
457+
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
458458
.on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)
459459
.on::<lsp_types::request::CodeLensResolve>(handlers::handle_code_lens_resolve)
460460
.on::<lsp_types::request::FoldingRangeRequest>(handlers::handle_folding_range)

0 commit comments

Comments
 (0)