use std::path::PathBuf;
use std::sync::Arc;
use cpp_parser::{CppDocComment, CppParseError, CppSyntaxNode, CppSyntaxTree, LineIndex};
use crate::file::coordinates::{FileOffset, ReadingOffset};
use crate::file::paths::FileId;
use crate::file::vfs::VfsFile;
use crate::symbol::ScopeTree;
#[derive(Debug, Clone)]
pub struct TokensOf {
pub path: PathBuf,
pub source: Arc<str>,
pub line_index: Arc<LineIndex>,
pub open: bool,
pub tokens: Vec<cpp_parser::CppTokenData>,
}
impl TokensOf {
pub fn token_at(&self, offset: ReadingOffset) -> Option<&cpp_parser::CppTokenData> {
let offset = offset.get();
self.tokens
.iter()
.find(|token| token.range.start_offset <= offset && offset < token.range.end_offset())
}
pub fn text_of(&self, range: cpp_parser::SourceRange) -> Option<&str> {
self.source.get(range.start_offset..range.end_offset())
}
pub fn offset_at(&self, line: usize, column: usize) -> Option<ReadingOffset> {
self.line_index
.get_offset(line, column, &self.source)
.map(|at| ReadingOffset::new(usize::from(at)))
}
pub fn position_at(&self, offset: ReadingOffset) -> Option<(usize, usize)> {
let offset = offset.get();
self.line_index.position_of(offset, &self.source)
}
pub fn source(&self) -> &str {
&self.source
}
}
#[derive(Debug, Clone)]
pub struct FileView {
pub file: FileId,
pub path: PathBuf,
pub source: Arc<str>,
pub line_index: Arc<LineIndex>,
pub tree: std::sync::Arc<CppSyntaxTree>,
pub root: CppSyntaxNode,
pub scopes: std::sync::Arc<ScopeTree>,
pub open: bool,
written: Option<Arc<str>>,
written_lines: Option<Arc<LineIndex>>,
reading: Arc<[crate::preprocess::cooked::RenderedSpan]>,
}
#[derive(Debug, Clone)]
pub struct ParsedView {
pub file: FileId,
pub path: PathBuf,
pub source: Arc<str>,
pub line_index: Arc<LineIndex>,
pub tree: std::sync::Arc<CppSyntaxTree>,
pub scopes: std::sync::Arc<ScopeTree>,
pub open: bool,
pub written: Option<Arc<str>>,
pub written_lines: Option<Arc<LineIndex>>,
pub reading: Arc<[crate::preprocess::cooked::RenderedSpan]>,
}
impl FileView {
pub fn into_parsed(self) -> ParsedView {
ParsedView {
file: self.file,
path: self.path,
source: self.source,
line_index: self.line_index,
tree: self.tree,
scopes: self.scopes,
open: self.open,
written: self.written,
written_lines: self.written_lines,
reading: self.reading,
}
}
pub fn from_parsed(parsed: ParsedView) -> FileView {
let root = parsed.tree.get_red_root();
FileView {
file: parsed.file,
path: parsed.path,
source: parsed.source,
line_index: parsed.line_index,
tree: parsed.tree,
root,
scopes: parsed.scopes,
open: parsed.open,
written: parsed.written,
written_lines: parsed.written_lines,
reading: parsed.reading,
}
}
}
impl FileView {
pub fn file_offset_of(&self, in_the_reading: ReadingOffset) -> Option<FileOffset> {
let in_the_reading = in_the_reading.get();
if self.written.is_none() {
return Some(FileOffset::new(in_the_reading));
}
Some(FileOffset::new(
self.span_at(in_the_reading)?.reported.start_offset,
))
}
pub fn file_offset_of_end(&self, in_the_reading: ReadingOffset) -> Option<FileOffset> {
let in_the_reading = in_the_reading.get();
if self.written.is_none() {
return Some(FileOffset::new(in_the_reading));
}
if in_the_reading == 0 {
return Some(FileOffset::new(0));
}
let last = self.span_at(in_the_reading - 1)?;
let into_the_span = (in_the_reading - 1).saturating_sub(last.cooked.start_offset);
Some(FileOffset::new(
last.reported.start_offset + into_the_span + 1,
))
}
pub fn file_range_of(
&self,
in_the_reading: cpp_parser::SourceRange,
) -> Option<cpp_parser::SourceRange> {
if self.written.is_none() {
return Some(in_the_reading);
}
let mut bounds: Option<(usize, usize)> = None;
for span in self
.reading
.iter()
.skip_while(|span| span.cooked.end_offset() <= in_the_reading.start_offset)
.take_while(|span| span.cooked.start_offset < in_the_reading.end_offset())
{
let reported = &span.reported;
bounds = Some(match bounds {
Some((start, end)) => (
start.min(reported.start_offset),
end.max(reported.end_offset()),
),
None => (reported.start_offset, reported.end_offset()),
});
}
let (start, end) = bounds?;
Some(cpp_parser::SourceRange::new(
start,
end.saturating_sub(start),
))
}
pub fn was_written_here(&self, in_the_reading: cpp_parser::SourceRange) -> bool {
if self.written.is_none() {
return true;
}
let mut covering = self
.reading
.iter()
.skip_while(|span| span.cooked.end_offset() <= in_the_reading.start_offset)
.take_while(|span| span.cooked.start_offset < in_the_reading.end_offset());
let mut any = false;
for span in covering.by_ref() {
any = true;
if span.written.is_none() {
return false;
}
}
any
}
pub fn reading_offset_of(&self, in_the_file: FileOffset) -> Option<ReadingOffset> {
let in_the_file = in_the_file.get();
if self.written.is_none() {
return Some(ReadingOffset::new(in_the_file));
}
let span = self
.reading
.iter()
.find(|span| {
span.written.as_ref().is_some_and(|written| {
written.start_offset <= in_the_file && in_the_file < written.end_offset()
})
})
.or_else(|| {
self.reading
.iter()
.find(|span| span.reported.end_offset() > in_the_file)
})?;
if let Some(previous) = self
.reading
.iter()
.rev()
.find(|span| span.reported.end_offset() == in_the_file)
{
return Some(ReadingOffset::new(previous.cooked.end_offset()));
}
let inside = match span.written {
Some(written) if written.start_offset <= in_the_file => {
in_the_file - written.start_offset
}
_ => 0,
};
let mapped = span.cooked.start_offset
+ inside.min(span.cooked.end_offset() - span.cooked.start_offset);
(mapped <= self.source.len()).then(|| ReadingOffset::new(mapped))
}
fn span_at(&self, in_the_reading: usize) -> Option<&crate::preprocess::cooked::RenderedSpan> {
let at = self
.reading
.partition_point(|span| span.cooked.end_offset() <= in_the_reading);
self.reading.get(at)
}
pub fn the_written_text(&self) -> Option<String> {
self.written_text()
.map(str::to_string)
.or_else(|| Some(self.root.text().to_string()))
}
pub fn written_text(&self) -> Option<&str> {
self.written.as_deref()
}
pub fn written_lines(&self) -> &LineIndex {
self.written_lines
.as_deref()
.unwrap_or(self.line_index.as_ref())
}
pub fn file_offset_at(&self, line: usize, column: usize) -> Option<FileOffset> {
match (&self.written, &self.written_lines) {
(Some(text), Some(index)) => index
.get_offset(line, column, text)
.map(|at| FileOffset::new(usize::from(at))),
_ => self
.offset_at(line, column)
.map(|at| FileOffset::new(at.get())),
}
}
}
pub fn documentation_at(root: &CppSyntaxNode, offset: usize) -> Option<CppDocComment> {
let ancestors = match cpp_parser::token_at(root, offset) {
Some(token) => token.parent_ancestors().collect::<Vec<_>>(),
None => root.ancestors().collect(),
};
ancestors
.into_iter()
.find_map(|node| cpp_parser::documentation_of(&node))
}
pub fn in_prose(text: &str, offset: usize) -> bool {
let (tokens, _) = cpp_parser::lex(text, &cpp_parser::LexerConfig::default());
let at = tokens
.iter()
.find(|token| token.range.start_offset <= offset && offset < token.range.end_offset())
.or_else(|| {
tokens
.iter()
.find(|token| token.range.end_offset() == offset)
});
at.is_some_and(|token| {
matches!(
token.kind,
cpp_parser::CppTokenKind::LineComment
| cpp_parser::CppTokenKind::BlockComment
| cpp_parser::CppTokenKind::StringLiteral
| cpp_parser::CppTokenKind::CharLiteral
)
})
}
pub fn might_be_documented(text: &str, offset: usize) -> bool {
let before = text
.get(..offset.min(text.len()))
.unwrap_or_default()
.trim_end();
if before.ends_with("*/") {
return true;
}
let at_a_line_start = offset == 0 || text.as_bytes().get(offset - 1) == Some(&b'\n');
let last_line = before.lines().last().unwrap_or_default().trim();
let the_last_line_is_evidence =
at_a_line_start || last_line.starts_with("//") || last_line.contains("*/");
let before = if the_last_line_is_evidence {
before
} else {
match before.rfind('\n') {
Some(newline) => before[..newline].trim_end(),
None => before,
}
};
let mut rest = before;
for _ in 0..12 {
let line = match rest.rfind('\n') {
Some(newline) => &rest[newline + 1..],
None => rest,
};
let line = line.trim();
if !line.is_empty() && !line.starts_with('#') {
if line.starts_with("//") || line.contains("*/") {
return true;
}
if line.ends_with(';') || line.ends_with('{') || line.ends_with('}') {
return false;
}
}
match rest.rfind('\n') {
Some(newline) => rest = &rest[..newline],
None => return false,
}
}
false
}
impl FileView {
#[deprecated(
note = "there is no semantic view of unexpanded text; use Session::view for meaning \
(and defer when it answers None) or Session::tokens_of for positions"
)]
pub fn parse(file: &VfsFile) -> Option<FileView> {
let _ = file;
None
}
pub fn parse_with_cuda(
file: &VfsFile,
macros: &cpp_parser::MacroEnvironment,
cuda: bool,
) -> FileView {
let source = file.text.clone();
let config = cpp_parser::ParserConfig::default()
.with_cuda(cuda)
.with_macros_from_includes(macros);
let tree = cpp_parser::CppParser::parse(&source, config);
let root = tree.get_red_root();
let scopes = crate::sema::scopes::build_scopes(&root, macros);
FileView {
file: file.id,
path: file.path.clone(),
source,
line_index: file.line_index.clone(),
tree: std::sync::Arc::new(tree),
root,
scopes: std::sync::Arc::new(scopes),
open: file.open,
written: None,
written_lines: None,
reading: Arc::from(Vec::new()),
}
}
pub fn parse_with(file: &VfsFile, macros: &cpp_parser::MacroEnvironment) -> FileView {
Self::parse_with_cuda(file, macros, false)
}
pub fn parse_rendering(
file: &VfsFile,
rendered: &crate::preprocess::cooked::RenderedCooked,
) -> FileView {
Self::parse_rendering_with_cuda(file, rendered, false)
}
pub fn parse_rendering_with_cuda(
file: &VfsFile,
rendered: &crate::preprocess::cooked::RenderedCooked,
cuda: bool,
) -> FileView {
Self::parse_rendering_with_the_files_own_language(file, rendered, cuda, None)
}
pub fn parse_rendering_in(
file: &VfsFile,
rendered: &crate::preprocess::cooked::RenderedCooked,
config: &crate::CompilerConfig,
) -> FileView {
Self::parse_rendering_with_the_files_own_language(
file,
rendered,
config.cuda(),
Some(config.language_for(&file.path)),
)
}
fn parse_rendering_with_the_files_own_language(
file: &VfsFile,
rendered: &crate::preprocess::cooked::RenderedCooked,
cuda: bool,
language: Option<cpp_parser::features::LanguageConfig>,
) -> FileView {
let source: Arc<str> = Arc::from(rendered.text.as_str());
let line_index = Arc::new(LineIndex::parse(&source));
let mut parser_config = cpp_parser::ParserConfig::default().with_cuda(cuda);
if let Some(language) = language {
parser_config = parser_config.with_language(language);
}
let tree = cpp_parser::CppParser::parse(&source, parser_config);
let root = tree.get_red_root();
let scopes = crate::sema::scopes::build_scopes(&root, &crate::sema::scopes::NoMacroBodies);
FileView {
file: file.id,
path: file.path.clone(),
source,
line_index,
tree: std::sync::Arc::new(tree),
root,
scopes: std::sync::Arc::new(scopes),
open: file.open,
written: Some(file.text.clone()),
written_lines: Some(file.line_index.clone()),
reading: Arc::from(rendered.spans.as_slice()),
}
}
pub fn errors(&self) -> &[CppParseError] {
self.tree.get_errors()
}
pub fn selection_chain(&self, offset: ReadingOffset) -> Vec<cpp_parser::SourceRange> {
selection_chain_of(&self.root, offset.get())
}
}
pub(crate) fn selection_chain_of(
root: &CppSyntaxNode,
offset: usize,
) -> Vec<cpp_parser::SourceRange> {
let mut chain: Vec<cpp_parser::SourceRange> = Vec::new();
let add = |range: cpp_parser::SourceRange, chain: &mut Vec<cpp_parser::SourceRange>| {
if range.length == 0 {
return;
}
if offset < range.start_offset || offset >= range.end_offset() {
return;
}
let already_covered = chain.last().is_some_and(|previous| {
range.start_offset >= previous.start_offset
&& range.end_offset() <= previous.end_offset()
});
if !already_covered {
chain.push(range);
}
};
let token = cpp_parser::token_at(root, offset);
let ancestors = match &token {
Some(token) => {
if !cpp_parser::is_trivia(token.kind().into()) {
add(cpp_parser::source_range(token.text_range()), &mut chain);
}
token
.parent_ancestors()
.map(|node| cpp_parser::source_range(node.text_range()))
.collect::<Vec<_>>()
}
None => root
.ancestors()
.map(|node| cpp_parser::source_range(node.text_range()))
.collect(),
};
for range in ancestors {
add(range, &mut chain);
}
chain
}
impl FileView {
pub fn offset_at(&self, line: usize, column: usize) -> Option<ReadingOffset> {
self.line_index
.get_offset(line, column, &self.source)
.map(|at| ReadingOffset::new(usize::from(at)))
}
pub fn position_at(&self, offset: ReadingOffset) -> Option<(usize, usize)> {
let offset = offset.get();
self.line_index.position_of(offset, &self.source)
}
pub fn source(&self) -> &str {
&self.source
}
pub fn get_token_text(&self, range: cpp_parser::SourceRange) -> Option<&str> {
self.source.get(range.start_offset..range.end_offset())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::file::paths::MemoryFiles;
use crate::file::vfs::Vfs;
fn tokens(vfs: &mut Vfs<MemoryFiles>, path: &str) -> (TokensOf, VfsFile) {
let file = vfs.file(path).expect("the file reads");
let tokens = TokensOf {
path: file.path.clone(),
source: file.text.clone(),
line_index: file.line_index.clone(),
open: file.open,
tokens: cpp_parser::lex(&file.text, &cpp_parser::LexerConfig::default()).0,
};
(tokens, file)
}
#[test]
fn a_view_maps_positions_through_the_index_the_vfs_built() {
let mut vfs =
Vfs::new(MemoryFiles::new().with_file("/p/a.cpp", "int a;\nint b;\nint c;\n"));
let (tokens, file) = tokens(&mut vfs, "/p/a.cpp");
assert!(
Arc::ptr_eq(&tokens.line_index, &file.line_index),
"and it shares that file's index rather than making one"
);
assert!(Arc::ptr_eq(&tokens.source, &file.text));
assert_eq!(tokens.offset_at(2, 4), Some(ReadingOffset::new(18)));
assert_eq!(tokens.position_at(ReadingOffset::new(18)), Some((2, 4)));
assert_eq!(tokens.offset_at(9, 0), None);
let covered: String = tokens
.tokens
.iter()
.map(|t| &tokens.source[t.range.start_offset..t.range.end_offset()])
.collect();
assert_eq!(
covered,
tokens.source(),
"the tokens cover the file byte for byte"
);
}
#[test]
fn a_view_of_an_edited_buffer_maps_positions_in_the_edited_text() {
let mut vfs = Vfs::new(MemoryFiles::new().with_file("/p/a.cpp", "int a;\n"));
let (before, _) = tokens(&mut vfs, "/p/a.cpp");
assert_eq!(
before.offset_at(1, 0),
Some(ReadingOffset::new(7)),
"a text that ends in a newline has an empty last line, at its end"
);
assert_eq!(before.offset_at(2, 0), None, "and no line after that");
vfs.insert("/p/a.cpp", "int a;\nint b;\n", true);
let (after, _) = tokens(&mut vfs, "/p/a.cpp");
assert!(after.open, "and the reading says the text is a buffer");
assert_eq!(
after.offset_at(1, 4),
Some(ReadingOffset::new(11)),
"`b` of the new second line"
);
assert_eq!(
before.offset_at(1, 4),
Some(ReadingOffset::new(7)),
"the older generation's line 1 was the empty line after the final newline, so column 4 clamps to its \
start — the end of the text"
);
}
}