mod hover;
mod navigate;
mod position;
mod tokens;
use core::ops::Range;
pub use crate::composer::complete::{Item, ItemKind};
pub use hover::{Hover, HoverContext};
pub use navigate::{DefKind, Definition, NoteTime};
pub use position::{byte_to_char, char_to_byte, LineIndex, Location};
pub use tokens::{tokens, tokens_with, Token, TokenKind};
use crate::composer::complete::{completions_with, CompleteOptions};
use crate::synth::SynthSpec;
use crate::{Ast, CompileOptions, Diagnostic, Song, Span};
#[derive(Debug, Clone)]
pub struct Analysis {
version: u64,
source: String,
index: LineIndex,
tokens: Vec<Token>,
diagnostics: Vec<Diagnostic>,
ast: Ast,
song: Option<Song>,
notes: Vec<NoteTime>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Completion {
pub replace: Range<usize>,
pub replace_chars: Range<usize>,
pub items: Vec<Item>,
}
impl Analysis {
pub fn new(source: &str, version: u64, opts: &CompileOptions<'_>) -> Self {
let index = LineIndex::new(source);
let tokens = tokens_with(source, &index);
let (ast, song, diagnostics) = crate::compile_parts(source, opts);
let notes = song.as_ref().map(navigate::note_times).unwrap_or_default();
Analysis {
version,
source: source.to_string(),
index,
tokens,
diagnostics,
ast,
song,
notes,
}
}
pub fn version(&self) -> u64 {
self.version
}
pub fn source(&self) -> &str {
&self.source
}
pub fn line_index(&self) -> &LineIndex {
&self.index
}
pub fn tokens(&self) -> &[Token] {
&self.tokens
}
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
pub fn ast(&self) -> &Ast {
&self.ast
}
pub fn song(&self) -> Option<&Song> {
self.song.as_ref()
}
pub fn note_times(&self) -> &[NoteTime] {
&self.notes
}
pub fn source_name(&self, source: u16) -> Option<&str> {
let i = (source as usize).checked_sub(1)?;
self.ast.sources.get(i).map(String::as_str)
}
pub fn location(&self, span: Span) -> Location {
let bytes = if span.source == 0 {
self.index.range_of(&self.source, span)
} else {
None
};
Location { span, bytes }
}
pub fn token_at(&self, byte: usize) -> Option<&Token> {
let i = self.tokens.partition_point(|t| t.bytes.end < byte);
let t = self.tokens.get(i)?;
(t.bytes.start <= byte && byte <= t.bytes.end).then_some(t)
}
pub fn line_text(&self, line: u32) -> &str {
self.index
.line_range(&self.source, line)
.map(|r| &self.source[r])
.unwrap_or("")
}
pub fn complete(&self, cursor: usize, opts: &CompileOptions<'_>, explicit: bool) -> Completion {
let chars = byte_to_char(&self.source, cursor);
let c = completions_with(&self.source, chars, opts, &CompleteOptions { explicit });
Completion {
replace: char_to_byte(&self.source, c.replace.start)
..char_to_byte(&self.source, c.replace.end),
replace_chars: c.replace,
items: c.items,
}
}
pub fn hover(&self, byte: usize, ctx: &HoverContext<'_>) -> Option<Hover> {
hover::hover(self, byte, ctx)
}
pub fn definition(&self, byte: usize) -> Option<Definition> {
navigate::definition_at(self, byte)
}
pub fn notes_at(&self, byte: usize) -> Vec<&NoteTime> {
let (line, col) = self.index.line_col(&self.source, byte);
let (line, col) = match self.token_at(byte) {
Some(t) if t.kind == TokenKind::Note => (t.span.line, t.span.col),
_ => (line, col),
};
self.notes
.iter()
.filter(|n| n.span.source == 0 && navigate::span_contains(n.span, line, col))
.collect()
}
pub fn sounding_at(&self, tick: u64) -> Vec<&NoteTime> {
self.notes
.iter()
.take_while(|n| n.tick <= tick)
.filter(|n| tick < n.end)
.collect()
}
pub fn synth_spec<'s>(&self, synths: &[&'s SynthSpec]) -> Option<&'s SynthSpec> {
let name = self.ast.synth.as_ref().map_or("basic", |s| s.name.as_str());
crate::synth::find(synths, name)
}
}