From 267a89bca2b606faafacfe69db7fda1ef27bb39f Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Wed, 2 Jan 2019 23:24:58 +0300
Subject: use LocalSyntaxPtr for file symbol

---
 crates/ra_analysis/src/imp.rs          | 84 +++++++++++++++++-----------------
 crates/ra_analysis/src/lib.rs          |  4 +-
 crates/ra_analysis/src/symbol_index.rs | 14 ++----
 3 files changed, 49 insertions(+), 53 deletions(-)

(limited to 'crates')

diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 5f67c95f6..7604c7def 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -360,52 +360,52 @@ impl db::RootDatabase {
         // Resolve the function's NameRef (NOTE: this isn't entirely accurate).
         let file_symbols = self.index_resolve(name_ref)?;
         for (fn_file_id, fs) in file_symbols {
-            if fs.kind == FN_DEF {
+            if fs.ptr.kind() == FN_DEF {
                 let fn_file = self.source_file(fn_file_id);
-                if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
-                    let descr = ctry!(source_binder::function_from_source(
-                        self, fn_file_id, fn_def
-                    )?);
-                    if let Some(descriptor) = descr.signature_info(self) {
-                        // If we have a calling expression let's find which argument we are on
-                        let mut current_parameter = None;
-
-                        let num_params = descriptor.params.len();
-                        let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some();
-
-                        if num_params == 1 {
-                            if !has_self {
-                                current_parameter = Some(0);
-                            }
-                        } else if num_params > 1 {
-                            // Count how many parameters into the call we are.
-                            // TODO: This is best effort for now and should be fixed at some point.
-                            // It may be better to see where we are in the arg_list and then check
-                            // where offset is in that list (or beyond).
-                            // Revisit this after we get documentation comments in.
-                            if let Some(ref arg_list) = calling_node.arg_list() {
-                                let start = arg_list.syntax().range().start();
-
-                                let range_search = TextRange::from_to(start, position.offset);
-                                let mut commas: usize = arg_list
-                                    .syntax()
-                                    .text()
-                                    .slice(range_search)
-                                    .to_string()
-                                    .matches(',')
-                                    .count();
-
-                                // If we have a method call eat the first param since it's just self.
-                                if has_self {
-                                    commas += 1;
-                                }
-
-                                current_parameter = Some(commas);
-                            }
+                let fn_def = fs.ptr.resolve(&fn_file);
+                let fn_def = ast::FnDef::cast(fn_def.borrowed()).unwrap();
+                let descr = ctry!(source_binder::function_from_source(
+                    self, fn_file_id, fn_def
+                )?);
+                if let Some(descriptor) = descr.signature_info(self) {
+                    // If we have a calling expression let's find which argument we are on
+                    let mut current_parameter = None;
+
+                    let num_params = descriptor.params.len();
+                    let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some();
+
+                    if num_params == 1 {
+                        if !has_self {
+                            current_parameter = Some(0);
                         }
+                    } else if num_params > 1 {
+                        // Count how many parameters into the call we are.
+                        // TODO: This is best effort for now and should be fixed at some point.
+                        // It may be better to see where we are in the arg_list and then check
+                        // where offset is in that list (or beyond).
+                        // Revisit this after we get documentation comments in.
+                        if let Some(ref arg_list) = calling_node.arg_list() {
+                            let start = arg_list.syntax().range().start();
+
+                            let range_search = TextRange::from_to(start, position.offset);
+                            let mut commas: usize = arg_list
+                                .syntax()
+                                .text()
+                                .slice(range_search)
+                                .to_string()
+                                .matches(',')
+                                .count();
+
+                            // If we have a method call eat the first param since it's just self.
+                            if has_self {
+                                commas += 1;
+                            }
 
-                        return Ok(Some((descriptor, current_parameter)));
+                            current_parameter = Some(commas);
+                        }
                     }
+
+                    return Ok(Some((descriptor, current_parameter)));
                 }
             }
         }
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index 8247914c0..69d6754d6 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -231,9 +231,9 @@ impl NavigationTarget {
     fn from_symbol(file_id: FileId, symbol: FileSymbol) -> NavigationTarget {
         NavigationTarget {
             name: symbol.name.clone(),
-            kind: symbol.kind.clone(),
+            kind: symbol.ptr.kind(),
             file_id,
-            range: symbol.node_range.clone(),
+            range: symbol.ptr.range(),
         }
     }
     pub fn name(&self) -> &SmolStr {
diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs
index 65abaec2e..1b6815bbf 100644
--- a/crates/ra_analysis/src/symbol_index.rs
+++ b/crates/ra_analysis/src/symbol_index.rs
@@ -5,12 +5,12 @@ use std::{
 
 use fst::{self, Streamer};
 use ra_syntax::{
-    SyntaxNodeRef, SourceFileNode, SmolStr, TextRange,
+    SyntaxNodeRef, SourceFileNode, SmolStr,
     algo::visit::{visitor, Visitor},
     SyntaxKind::{self, *},
     ast::{self, NameOwner},
 };
-use ra_db::{SyntaxDatabase, SourceRootId, FilesDatabase};
+use ra_db::{SyntaxDatabase, SourceRootId, FilesDatabase, LocalSyntaxPtr};
 use salsa::ParallelDatabase;
 use rayon::prelude::*;
 
@@ -140,7 +140,7 @@ impl Query {
                 let idx = indexed_value.value as usize;
 
                 let (file_id, symbol) = &file_symbols.symbols[idx];
-                if self.only_types && !is_type(symbol.kind) {
+                if self.only_types && !is_type(symbol.ptr.kind()) {
                     continue;
                 }
                 if self.exact && symbol.name != self.query {
@@ -163,9 +163,7 @@ fn is_type(kind: SyntaxKind) -> bool {
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub(crate) struct FileSymbol {
     pub(crate) name: SmolStr,
-    pub(crate) node_range: TextRange,
-    pub(crate) kind: SyntaxKind,
-    _x: (),
+    pub(crate) ptr: LocalSyntaxPtr,
 }
 
 fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
@@ -173,9 +171,7 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
         let name = node.name()?;
         Some(FileSymbol {
             name: name.text(),
-            node_range: node.syntax().range(),
-            kind: node.syntax().kind(),
-            _x: (),
+            ptr: LocalSyntaxPtr::new(node.syntax()),
         })
     }
     visitor()
-- 
cgit v1.2.3