Skip to content

Commit

Permalink
Add hover for macro documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed Jan 31, 2023
1 parent 7a2f3b0 commit 75c989b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
12 changes: 9 additions & 3 deletions crates/dm-langserver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,13 @@ handle_method_call! {
let next = self.find_scoped_type(&iter, priors);
results.append(&mut self.construct_var_hover(var_name, next, true)?);
}
Annotation::MacroUse { docs, .. } => {
if let Some(dc) = docs {
if !dc.is_empty() {
results.push(dc.text());
}
}
}
_ => {}
}
}
Expand Down Expand Up @@ -1536,9 +1543,8 @@ handle_method_call! {
}
}
},
Annotation::MacroUse(name, location) => {
// TODO: get docs for this macro
results.push(self.convert_location(*location, &Default::default(), &["/DM/preprocessor/", name])?);
Annotation::MacroUse { name, definition_location, .. } => {
results.push(self.convert_location(*definition_location, &Default::default(), &["/DM/preprocessor/", name])?);
},
}

Expand Down
10 changes: 9 additions & 1 deletion crates/dreammaker/src/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! Data structures for the parser to output mappings from input ranges to AST
//! elements at those positions.
use std::rc::Rc;

use interval_tree::{IntervalTree, RangePairIter, RangeInclusive, range};
use crate::docs::DocCollection;

use super::Location;
use super::ast::*;

Expand Down Expand Up @@ -29,7 +33,11 @@ pub enum Annotation {

// a macro is called here, which is defined at this location
MacroDefinition(Ident),
MacroUse(String, Location),
MacroUse {
name: String,
definition_location: Location,
docs: Option<Rc<DocCollection>>,
},

Include(std::path::PathBuf),
Resource(std::path::PathBuf),
Expand Down
30 changes: 18 additions & 12 deletions crates/dreammaker/src/preprocessor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The preprocessor.
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
use std::{io, fmt};
use std::fs::File;
use std::path::{Path, PathBuf};
Expand All @@ -25,13 +26,13 @@ const MAX_RECURSION_DEPTH: usize = 32;
pub enum Define {
Constant {
subst: Vec<Token>,
docs: DocCollection,
docs: Rc<DocCollection>,
},
Function {
params: Vec<Ident>,
subst: Vec<Token>,
variadic: bool,
docs: DocCollection,
docs: Rc<DocCollection>,
},
}

Expand Down Expand Up @@ -532,15 +533,20 @@ impl<'ctx> Preprocessor<'ctx> {
// ------------------------------------------------------------------------
// Macro definition handling

fn annotate_macro(&mut self, ident: &str, def_loc: Location) {
fn annotate_macro(&mut self, ident: &str, definition_location: Location, docs: Option<Rc<DocCollection>>) {
if self.include_stack.in_expansion() {
return;
}

if let Some(annotations) = self.annotations.as_mut() {
annotations.insert(
self.last_input_loc .. self.last_input_loc.add_columns(ident.len() as u16),
Annotation::MacroUse(ident.to_owned(), def_loc));
Annotation::MacroUse {
name: ident.to_owned(),
definition_location,
docs,
}
);
}
}

Expand Down Expand Up @@ -897,9 +903,9 @@ impl<'ctx> Preprocessor<'ctx> {
}
}
let define = if params.is_empty() {
Define::Constant { subst, docs }
Define::Constant { subst, docs: Rc::new(docs) }
} else {
Define::Function { params, subst, variadic, docs }
Define::Function { params, subst, variadic, docs: Rc::new(docs) }
};
// DEBUG can only be defined in the root .dme file
if define_name != "DEBUG" || self.in_environment() {
Expand Down Expand Up @@ -972,7 +978,7 @@ impl<'ctx> Preprocessor<'ctx> {

// substitute special macros
if ident == "__FILE__" {
self.annotate_macro(ident, Location::builtins());
self.annotate_macro(ident, Location::builtins(), None);
for include in self.include_stack.stack.iter().rev() {
if let Include::File { ref path, .. } = *include {
self.output.push_back(Token::String(path.display().to_string()));
Expand All @@ -982,7 +988,7 @@ impl<'ctx> Preprocessor<'ctx> {
self.output.push_back(Token::String(String::new()));
return Ok(());
} else if ident == "__LINE__" {
self.annotate_macro(ident, Location::builtins());
self.annotate_macro(ident, Location::builtins(), None);
self.output.push_back(Token::Int(self.last_input_loc.line as i32));
return Ok(());
}
Expand All @@ -1008,16 +1014,16 @@ impl<'ctx> Preprocessor<'ctx> {
}

match expansion {
Some((location, Define::Constant { subst, docs: _ })) => {
self.annotate_macro(ident, location);
Some((location, Define::Constant { subst, docs })) => {
self.annotate_macro(ident, location, Some(docs.clone()));
self.include_stack.stack.push(Include::Expansion {
//name: ident.to_owned(),
tokens: subst.into_iter().collect(),
location: self.last_input_loc,
});
return Ok(());
}
Some((location, Define::Function { ref params, ref subst, variadic, docs: _ })) => {
Some((location, Define::Function { ref params, ref subst, variadic, docs })) => {
// if it's not followed by an LParen, it isn't really a function call
match next!() {
Token::Punct(Punctuation::LParen) => {}
Expand All @@ -1033,7 +1039,7 @@ impl<'ctx> Preprocessor<'ctx> {
}
}

self.annotate_macro(ident, location);
self.annotate_macro(ident, location, Some(docs.clone()));

// read arguments
let mut args = Vec::new();
Expand Down

0 comments on commit 75c989b

Please sign in to comment.