-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to print functions and their location
- Loading branch information
1 parent
3b36ecf
commit 2e789f8
Showing
11 changed files
with
328 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, StandardStreamLock, WriteColor}; | ||
|
||
use crate::traits::*; | ||
|
||
use crate::checker::Checker; | ||
use crate::getter::Getter; | ||
|
||
#[derive(Debug, Serialize)] | ||
pub struct FunctionSpan { | ||
pub name: String, | ||
pub start_line: usize, | ||
pub end_line: usize, | ||
pub error: bool, | ||
} | ||
|
||
pub fn function<T: TSParserTrait>(parser: &T) -> Vec<FunctionSpan> { | ||
let root = parser.get_root(); | ||
let code = parser.get_code(); | ||
let mut spans = Vec::new(); | ||
root.act_on_node(&mut |n| { | ||
if T::Checker::is_func(n) { | ||
let start_line = n.start_position().row + 1; | ||
let end_line = n.end_position().row + 1; | ||
if let Some(name) = T::Getter::get_func_name(n, code) { | ||
spans.push(FunctionSpan { | ||
name: name.to_string(), | ||
start_line, | ||
end_line, | ||
error: false, | ||
}); | ||
} else { | ||
spans.push(FunctionSpan { | ||
name: "".to_string(), | ||
start_line, | ||
end_line, | ||
error: true, | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
spans | ||
} | ||
|
||
fn dump_span( | ||
span: FunctionSpan, | ||
stdout: &mut StandardStreamLock, | ||
last: bool, | ||
) -> std::io::Result<()> { | ||
/*if !span.error { | ||
return Ok(()); | ||
}*/ | ||
|
||
let pref = if last { " `- " } else { " |- " }; | ||
|
||
color!(stdout, Blue); | ||
write!(stdout, "{}", pref)?; | ||
|
||
if span.error { | ||
color!(stdout, Red, true); | ||
write!(stdout, "error: ")?; | ||
} else { | ||
color!(stdout, Magenta, true); | ||
write!(stdout, "{}: ", span.name)?; | ||
} | ||
|
||
color!(stdout, Green); | ||
write!(stdout, "from line ")?; | ||
|
||
color!(stdout, White); | ||
write!(stdout, "{}", span.start_line)?; | ||
|
||
color!(stdout, Green); | ||
write!(stdout, " to line ")?; | ||
|
||
color!(stdout, White); | ||
writeln!(stdout, "{}.", span.end_line) | ||
} | ||
|
||
fn dump_spans(mut spans: Vec<FunctionSpan>, path: PathBuf) -> std::io::Result<()> { | ||
if !spans.is_empty() { | ||
let stdout = StandardStream::stdout(ColorChoice::Always); | ||
let mut stdout = stdout.lock(); | ||
|
||
color!(stdout, Yellow, true); | ||
writeln!(&mut stdout, "In file {}", path.to_str().unwrap_or("..."))?; | ||
|
||
for span in spans.drain(..spans.len() - 1) { | ||
dump_span(span, &mut stdout, false)?; | ||
} | ||
dump_span(spans.pop().unwrap(), &mut stdout, true)?; | ||
color!(stdout, White); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub struct FunctionCfg { | ||
pub path: PathBuf, | ||
} | ||
|
||
pub struct Function {} | ||
|
||
impl Callback for Function { | ||
type Res = std::io::Result<()>; | ||
type Cfg = FunctionCfg; | ||
|
||
fn call<T: TSParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res { | ||
dump_spans(function(parser), cfg.path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{self, Value}; | ||
|
||
use crate::function::{function, FunctionSpan}; | ||
use crate::traits::{Callback, TSParserTrait}; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct WebFunctionPayload { | ||
pub id: String, | ||
pub file_name: String, | ||
pub code: String, | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
pub struct WebFunctionResponse { | ||
pub id: String, | ||
pub spans: Vec<FunctionSpan>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct WebFunctionInfo { | ||
pub file_name: String, | ||
} | ||
|
||
pub struct WebFunctionCallback {} | ||
|
||
pub struct WebFunctionCfg { | ||
pub id: String, | ||
} | ||
|
||
impl Callback for WebFunctionCallback { | ||
type Res = Value; | ||
type Cfg = WebFunctionCfg; | ||
|
||
fn call<T: TSParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res { | ||
let spans = function(parser); | ||
serde_json::to_value(WebFunctionResponse { id: cfg.id, spans }).unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod alterator; | ||
pub mod ast; | ||
pub mod comment; | ||
pub mod function; | ||
pub mod metrics; | ||
pub mod server; |
Oops, something went wrong.