Skip to content
Merged
31 changes: 27 additions & 4 deletions src/document_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use lsp_types::TextDocumentContentChangeEvent;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use url::Url;

use crate::parser::tokens::{PhpClassName, PhpMethod, Token, TokenData};
use crate::parser::tokens::{
ClassAttribute, DrupalPluginReference, PhpClassName, PhpMethod, Token, TokenData,
};

use self::document::{Document, FileType};

Expand All @@ -37,7 +39,7 @@ pub fn initialize_document_store(root_dir: String) {
override_builder
.add("!**/core/lib/**/*Interface.php")
.unwrap();
override_builder.add("!**/Plugin/**/*.php").unwrap();
override_builder.add("!**/tests/**/*.php").unwrap();
override_builder.add("!vendor").unwrap();
override_builder.add("!node_modules").unwrap();
override_builder.add("!libraries").unwrap();
Expand Down Expand Up @@ -195,7 +197,7 @@ impl DocumentStore {
}

pub fn get_method_definition(&self, method: &PhpMethod) -> Option<(&Document, &Token)> {
if let Some((document, token)) = self.get_class_definition(&method.class_name) {
if let Some((document, token)) = self.get_class_definition(&method.get_class(self)?) {
if let TokenData::PhpClassDefinition(class) = &token.data {
let token = class.methods.get(&method.name)?;
return Some((document, token));
Expand All @@ -222,7 +224,6 @@ impl DocumentStore {

pub fn get_permission_definition(&self, permission_name: &str) -> Option<(&Document, &Token)> {
let files = self.get_documents_by_file_type(FileType::Yaml);
log::info!("{}", permission_name);

files.iter().find_map(|&document| {
Some((
Expand All @@ -237,6 +238,28 @@ impl DocumentStore {
})
}

pub fn get_plugin_definition(
&self,
plugin_reference: &DrupalPluginReference,
) -> Option<(&Document, &Token)> {
let files = self.get_documents_by_file_type(FileType::Php);

files.iter().find_map(|&document| {
Some((
document,
document.tokens.iter().find(|token| {
if let TokenData::PhpClassDefinition(class) = &token.data {
if let Some(ClassAttribute::Plugin(plugin)) = &class.attribute {
return plugin.plugin_type == plugin_reference.plugin_type
&& plugin.plugin_id == plugin_reference.plugin_id;
}
}
false
})?,
))
})
}

fn get_documents_by_file_type(&self, file_type: FileType) -> Vec<&Document> {
self.documents
.values()
Expand Down
2 changes: 1 addition & 1 deletion src/documentation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn get_documentation_for_token(token: &Token) -> Option<String> {
}
TokenData::PhpMethodReference(method) => Some(format!(
"PHP Method reference\nclass: {}\nmethod: {}",
method.class_name, method.name
method.class_name.clone()?, method.name
)),
TokenData::DrupalRouteReference(route_name) => {
let store = DOCUMENT_STORE.lock().unwrap();
Expand Down
22 changes: 19 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use tree_sitter::Node;

pub mod yaml;
pub mod php;
pub mod tokens;
pub mod yaml;

use lsp_types::Position;
use tree_sitter::{Language, Node, Parser, Point, Tree};

pub fn get_closest_parent_by_kind<'a>(node: &'a Node, kind: &'a str) -> Option<Node<'a>> {
let mut parent = node.parent();
Expand All @@ -11,3 +12,18 @@ pub fn get_closest_parent_by_kind<'a>(node: &'a Node, kind: &'a str) -> Option<N
}
parent
}

pub fn get_tree(source: &str, language: &Language) -> Option<Tree> {
let mut parser = Parser::new();
parser.set_language(language).ok()?;
parser.parse(source.as_bytes(), None)
}

pub fn get_node_at_position(tree: &Tree, position: Position) -> Option<Node> {
let start = position_to_point(position);
tree.root_node().descendant_for_point_range(start, start)
}

pub fn position_to_point(position: Position) -> Point {
Point::new(position.line as usize, position.character as usize)
}
Loading
Loading