Add media semantic

This commit is contained in:
ef3d0c3e 2024-10-29 10:36:21 +01:00
parent f23f8db994
commit 3f49e8b8c2
2 changed files with 40 additions and 5 deletions

View file

@ -3,6 +3,8 @@ use std::rc::Rc;
use std::str::FromStr; use std::str::FromStr;
use ariadne::Fmt; use ariadne::Fmt;
use lsp::semantic::Semantics;
use parser::util::escape_source;
use regex::Captures; use regex::Captures;
use regex::Match; use regex::Match;
use regex::Regex; use regex::Regex;
@ -449,13 +451,30 @@ impl RegexRule for MediaRule {
.ok() .ok()
.map(|(_, value)| value); .map(|(_, value)| value);
if let Some((sems, tokens)) =
Semantics::from_source(token.source(), &state.shared.lsp)
{
sems.add(matches.get(0).unwrap().start()..matches.get(0).unwrap().start()+1, tokens.media_sep);
// Refname
sems.add(matches.get(0).unwrap().start()+1..matches.get(0).unwrap().start()+2, tokens.media_refname_sep);
sems.add(matches.get(1).unwrap().range(), tokens.media_refname);
sems.add(matches.get(1).unwrap().end()..matches.get(1).unwrap().end()+1, tokens.media_refname_sep);
// Uri
sems.add(matches.get(2).unwrap().start()-1..matches.get(2).unwrap().start(), tokens.media_uri_sep);
sems.add(matches.get(2).unwrap().range(), tokens.media_uri);
sems.add(matches.get(2).unwrap().end()..matches.get(2).unwrap().end()+1, tokens.media_uri_sep);
// Props
if let Some(props) = matches.get(3)
{
sems.add(props.start()-1..props.start(), tokens.media_props_sep);
sems.add(props.range(), tokens.media_props);
sems.add(props.end()..props.end()+1, tokens.media_props_sep);
}
}
let description = match matches.get(4) { let description = match matches.get(4) {
Some(content) => { Some(content) => {
let source = Rc::new(VirtualSource::new( let source = escape_source(token.source(), content.range(), format!("Media[{refname}] description"), '\\', "\n");
Token::new(content.range(), token.source()),
format!("Media[{refname}] description"),
content.as_str().trim_start().trim_end().to_string(),
));
if source.content().is_empty() { if source.content().is_empty() {
None None
} else { } else {

View file

@ -174,6 +174,14 @@ pub struct Tokens {
pub layout_props_sep: (u32, u32), pub layout_props_sep: (u32, u32),
pub layout_props: (u32, u32), pub layout_props: (u32, u32),
pub layout_type: (u32, u32), pub layout_type: (u32, u32),
pub media_sep: (u32, u32),
pub media_refname_sep: (u32, u32),
pub media_refname: (u32, u32),
pub media_uri_sep: (u32, u32),
pub media_uri: (u32, u32),
pub media_props_sep: (u32, u32),
pub media_props: (u32, u32),
} }
impl Tokens { impl Tokens {
@ -262,6 +270,14 @@ impl Tokens {
layout_props_sep: token!("operator"), layout_props_sep: token!("operator"),
layout_props: token!("enum"), layout_props: token!("enum"),
layout_type: token!("function"), layout_type: token!("function"),
media_sep: token!("macro"),
media_refname_sep: token!("macro"),
media_refname: token!("enum"),
media_uri_sep: token!("macro"),
media_uri: token!("function"),
media_props_sep: token!("operator"),
media_props: token!("enum"),
} }
} }
} }