mirror of
https://github.com/denoland/deno.git
synced 2025-09-30 14:11:14 +00:00
refactor(lsp): use deno_ast and cache swc ASTs (#11780)
This commit is contained in:
parent
a5bcf7033e
commit
2c2e3ec1ca
39 changed files with 1037 additions and 1758 deletions
76
cli/lsp/document_source.rs
Normal file
76
cli/lsp/document_source.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
use deno_ast::swc::common::BytePos;
|
||||
use deno_ast::Diagnostic;
|
||||
use deno_ast::MediaType;
|
||||
use deno_ast::ParsedSource;
|
||||
use deno_ast::SourceTextInfo;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::analysis;
|
||||
use super::text::LineIndex;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DocumentSourceInner {
|
||||
specifier: ModuleSpecifier,
|
||||
media_type: MediaType,
|
||||
text_info: SourceTextInfo,
|
||||
parsed_module: OnceCell<Result<ParsedSource, Diagnostic>>,
|
||||
line_index: LineIndex,
|
||||
}
|
||||
|
||||
/// Immutable information about a document.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DocumentSource {
|
||||
inner: Arc<DocumentSourceInner>,
|
||||
}
|
||||
|
||||
impl DocumentSource {
|
||||
pub fn new(
|
||||
specifier: &ModuleSpecifier,
|
||||
media_type: MediaType,
|
||||
text: Arc<String>,
|
||||
line_index: LineIndex,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(DocumentSourceInner {
|
||||
specifier: specifier.clone(),
|
||||
media_type,
|
||||
text_info: SourceTextInfo::new(BytePos(0), text),
|
||||
parsed_module: OnceCell::new(),
|
||||
line_index,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_info(&self) -> &SourceTextInfo {
|
||||
&self.inner.text_info
|
||||
}
|
||||
|
||||
pub fn line_index(&self) -> &LineIndex {
|
||||
&self.inner.line_index
|
||||
}
|
||||
|
||||
pub fn module(&self) -> Option<&Result<ParsedSource, Diagnostic>> {
|
||||
let is_parsable = matches!(
|
||||
self.inner.media_type,
|
||||
MediaType::JavaScript
|
||||
| MediaType::Jsx
|
||||
| MediaType::TypeScript
|
||||
| MediaType::Tsx
|
||||
| MediaType::Dts,
|
||||
);
|
||||
if is_parsable {
|
||||
// lazily parse the module
|
||||
Some(self.inner.parsed_module.get_or_init(|| {
|
||||
analysis::parse_module(
|
||||
&self.inner.specifier,
|
||||
self.inner.text_info.clone(),
|
||||
self.inner.media_type,
|
||||
)
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue