mod calls;
mod refs;
mod symbols;
use super::{auto_dts, load_config, warn_cargo_htl_mismatch};
use anyhow::{Context, Result};
use htl::Htl;
use htl::project::{self, Collect, Sink};
use lsp_server::{Connection, ErrorCode, Message, Notification, RequestId, Response};
use lsp_types::notification::{
Cancel, DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument,
DidOpenTextDocument, DidSaveTextDocument, Exit, Notification as _, PublishDiagnostics,
};
use lsp_types::request::{
CallHierarchyIncomingCalls, CallHierarchyOutgoingCalls, CallHierarchyPrepare,
CodeActionRequest, DocumentSymbolRequest, Formatting, GotoDefinition, HoverRequest, References,
RegisterCapability, Request as _, Shutdown, WorkspaceSymbolRequest,
};
use lsp_types::{
CallHierarchyIncomingCallsParams, CallHierarchyItem, CallHierarchyOutgoingCallsParams,
CallHierarchyPrepareParams, CallHierarchyServerCapability, CodeAction, CodeActionKind,
CodeActionOptions, CodeActionOrCommand, CodeActionParams, CodeActionProviderCapability,
CodeActionTriggerKind, DiagnosticRelatedInformation, DiagnosticSeverity,
DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
DidChangeWatchedFilesRegistrationOptions, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentFormattingParams, DocumentSymbol,
DocumentSymbolParams, DocumentSymbolResponse, FileSystemWatcher, GlobPattern,
GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams,
HoverProviderCapability, Location, MarkupContent, MarkupKind, NumberOrString, OneOf, Position,
PublishDiagnosticsParams, Range, ReferenceParams, Registration, RegistrationParams,
SaveOptions, ServerCapabilities, SymbolInformation, TextDocumentSyncCapability,
TextDocumentSyncKind, TextDocumentSyncOptions, TextDocumentSyncSaveOptions, TextEdit, Uri,
WorkspaceEdit, WorkspaceSymbolParams, WorkspaceSymbolResponse,
};
use std::collections::{HashMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::str::FromStr;
use symbols::{Entry, Stamp, SymbolIndex};
pub fn serve() -> Result<ExitCode> {
let (stdio, io_threads) = Connection::stdio();
let (tx, receiver) = crossbeam_channel::unbounded();
let source = stdio.receiver;
std::thread::spawn(move || {
for message in source {
if tx.send(message).is_err() {
break;
}
}
});
let connection = Connection {
sender: stdio.sender,
receiver,
};
let code = run_on(&connection)?;
drop(connection);
io_threads.join().context("closing stdio")?;
Ok(code)
}
fn run_on(connection: &Connection) -> Result<ExitCode> {
let (id, params) = connection
.initialize_start()
.context("waiting for the client's initialize request")?;
let dynamic_watch = params
.pointer("/capabilities/workspace/didChangeWatchedFiles/dynamicRegistration")
.and_then(serde_json::Value::as_bool)
== Some(true);
let capabilities = ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
open_close: Some(true),
change: Some(TextDocumentSyncKind::FULL),
save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
include_text: Some(false),
})),
will_save: None,
will_save_wait_until: None,
},
)),
document_formatting_provider: Some(OneOf::Left(true)),
hover_provider: Some(HoverProviderCapability::Simple(true)),
definition_provider: Some(OneOf::Left(true)),
document_symbol_provider: Some(OneOf::Left(true)),
workspace_symbol_provider: Some(OneOf::Left(true)),
references_provider: Some(OneOf::Left(true)),
call_hierarchy_provider: Some(CallHierarchyServerCapability::Simple(true)),
code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX, fix_all_kind()]),
work_done_progress_options: Default::default(),
resolve_provider: Some(false),
})),
..ServerCapabilities::default()
};
connection
.initialize_finish(
id,
serde_json::json!({
"capabilities": capabilities,
"serverInfo": { "name": "htl", "version": env!("CARGO_PKG_VERSION") },
}),
)
.context("finishing the handshake")?;
Server::new(connection, dynamic_watch, workspace_of(¶ms)).run()
}
struct Project {
cfg: project::Config,
model: Option<htl::model::Project>,
scope: project::Scope,
origins: project::Origins,
policy: htl::verdict::Policy,
checker: Htl,
fix: htl::fix::FixOptions,
fix_error: Option<String>,
manifests: Manifests,
}
struct Manifests {
seen: Vec<(PathBuf, Option<u64>)>,
}
impl Manifests {
fn names() -> [&'static str; 3] {
[
htl::config::CONFIG_NAME,
htl::pkg::MANIFEST_NAME,
htl::pkg::LOCKFILE_NAME,
]
}
fn read(root: &Path) -> Self {
let seen = Self::names()
.iter()
.map(|n| {
let p = root.join(n);
let h = hash_of(&p);
(p, h)
})
.collect();
Self { seen }
}
fn changed(&self) -> bool {
self.seen.iter().any(|(p, h)| hash_of(p) != *h)
}
}
fn hash_of(path: &Path) -> Option<u64> {
let bytes = std::fs::read(path).ok()?;
let mut h = DefaultHasher::new();
bytes.hash(&mut h);
Some(h.finish())
}
impl Project {
fn load(file: &Path, root: &Path) -> Result<Self> {
let manifests = Manifests::read(root);
let cfg = load_config(file)?;
warn_cargo_htl_mismatch(file);
if let Err(e) = auto_dts(file) {
eprintln!("htl lsp: {e:#}");
}
let model = project::model_of(&cfg, file)?;
let scope = project::Scope::new(&cfg, model.as_ref(), file, None)?;
let origins = project::Origins::new(model.as_ref());
let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
let checker = project::checker(
model.as_ref(),
scope.lints.selection(),
&project::lang_of(&cfg),
)?;
let fix = htl::fix::FixOptions {
unsafe_fixes: false,
promoted: cfg
.as_ref()
.map(|(_, _, c)| c.fix.unsafe_.clone())
.unwrap_or_default(),
disabled: cfg
.as_ref()
.map(|(_, _, c)| c.fix.disable.clone())
.unwrap_or_default(),
only: Vec::new(),
dry_run: true,
};
let fix_error = fix.validate().err().map(|e| format!("{e:#}"));
Ok(Self {
cfg,
model,
scope,
origins,
policy,
checker,
fix,
fix_error,
manifests,
})
}
fn format(&self, file: &Path, text: &str) -> Result<String> {
let indent = self
.cfg
.as_ref()
.and_then(|(_, _, c)| c.fmt.indent)
.unwrap_or(3);
self.checker.format_text(text, file, indent)
}
fn source_files(&self, root: &Path, open: &[PathBuf]) -> Result<Vec<PathBuf>> {
let Some(model) = self.model.as_ref() else {
return Ok(open.to_vec());
};
let paths = [root.to_path_buf()];
let skip = project::not_walked(Some(model), &paths, htl::model::Purpose::Check);
let (files, _outside) = project::held_by_modules(
Some(model),
&paths,
htl::collect_tl_skipping(&paths, &skip)?,
)?;
Ok(files)
}
fn symbols_of(
&self,
open: &[&Doc],
canonical: &Path,
path: &Path,
text: &str,
) -> Vec<symbols::Sym> {
let checked = open
.iter()
.find(|d| d.canonical == canonical)
.and_then(|d| d.checked.as_ref())
.filter(|c| c.text == text);
if let Some(decls) = checked.and_then(|c| c.types.declarations()) {
return symbols::flatten(text, decls);
}
match self.checker.declarations_of(path) {
Ok(Some(decls)) => symbols::flatten(text, &decls),
Ok(None) => Vec::new(),
Err(e) => {
eprintln!("htl lsp: no symbols for {}: {e:#}", path.display());
Vec::new()
}
}
}
fn fixed(&self, file: &Path) -> Result<Option<String>> {
let h = project::checker(
self.model.as_ref(),
self.scope.lints.selection(),
&project::lang_of(&self.cfg),
)?;
project::file_view(&h, self.model.as_ref(), file)?;
Ok(htl::fix::fix_file(&h, file, &self.fix)?.contents)
}
fn indexed(&self, file: &Path) -> Result<htl::CheckInfo> {
let h = &self.checker;
let saved = h.search_path()?;
let found = (|| {
project::file_view(h, self.model.as_ref(), file)?;
h.check_indexed(file)
})();
h.set_search_path(&saved)?;
found
}
fn check(&self, file: &Path, open: &[PathBuf]) -> Result<Done> {
let mut sink = Sink::new(Collect::default());
sink.walking(open);
let walk = self
.scope
.walk(&self.cfg, self.model.as_ref(), &self.origins);
let (c, _) = project::check_file(
&self.checker,
&mut sink,
file,
&walk,
project::Fresh::DiskIndexed,
)?;
Ok(Done {
found: sink.out().take(),
deps: c.deps,
requires: c.requires,
types: c.types,
})
}
}
struct Done {
found: Vec<htl::Diagnostic>,
deps: Vec<PathBuf>,
requires: Vec<htl::RequireSite>,
types: htl::typeindex::TypeIndex,
}
struct Doc {
path: PathBuf,
canonical: PathBuf,
project: Option<PathBuf>,
deps: HashSet<PathBuf>,
buffer: Option<String>,
unsynced: bool,
checked: Option<Checked>,
}
struct Checked {
text: String,
findings: Vec<Finding>,
requires: Vec<htl::RequireSite>,
types: htl::typeindex::TypeIndex,
fix_all: Option<Option<String>>,
}
struct Finding {
lsp: lsp_types::Diagnostic,
fix: Option<htl::Fix>,
rule: String,
}
#[derive(Default)]
struct Stale {
files: Vec<Uri>,
changed: Vec<PathBuf>,
}
struct Server<'c> {
connection: &'c Connection,
projects: HashMap<PathBuf, Project>,
open: HashMap<Uri, Doc>,
shutting_down: bool,
dynamic_watch: bool,
watching: bool,
workspace: Vec<PathBuf>,
indices: HashMap<PathBuf, SymbolIndex>,
}
const REGISTER_ID: &str = "htl-watched-files";
const WATCHED: [&str; 7] = [
"**/htl.toml",
"**/mlua-pkg.toml",
"**/mlua-pkg.lock",
"**/types/**/*.d.tl",
"**/*.tl",
"**/Cargo.toml",
"**/{src,examples,tests,benches}/**/*.rs",
];
impl<'c> Server<'c> {
fn new(connection: &'c Connection, dynamic_watch: bool, workspace: Vec<PathBuf>) -> Self {
Self {
connection,
projects: HashMap::new(),
open: HashMap::new(),
shutting_down: false,
dynamic_watch,
watching: false,
workspace,
indices: HashMap::new(),
}
}
fn register_watchers(&self) {
let options = DidChangeWatchedFilesRegistrationOptions {
watchers: WATCHED
.iter()
.map(|g| FileSystemWatcher {
glob_pattern: GlobPattern::String((*g).to_string()),
kind: None,
})
.collect(),
};
let params = RegistrationParams {
registrations: vec![Registration {
id: REGISTER_ID.to_string(),
method: DidChangeWatchedFiles::METHOD.to_string(),
register_options: serde_json::to_value(options).ok(),
}],
};
let request = lsp_server::Request::new(
RequestId::from(REGISTER_ID.to_string()),
RegisterCapability::METHOD.to_string(),
params,
);
self.send(request.into());
}
fn run(&mut self) -> Result<ExitCode> {
if self.dynamic_watch {
self.register_watchers();
}
loop {
let Ok(first) = self.connection.receiver.recv() else {
eprintln!("htl lsp: the client closed the connection without `exit`");
return Ok(ExitCode::FAILURE);
};
let mut batch = vec![first];
while let Ok(next) = self.connection.receiver.try_recv() {
batch.push(next);
}
let mut stale = Stale::default();
for message in batch {
if !self.shutting_down
&& matches!(&message, Message::Request(r) if r.method == Shutdown::METHOD)
{
self.check_all(&std::mem::take(&mut stale));
}
if let Some(code) = self.apply(message, &mut stale) {
return Ok(code);
}
}
if !self.shutting_down {
self.check_all(&stale);
}
}
}
fn apply(&mut self, message: Message, stale: &mut Stale) -> Option<ExitCode> {
match message {
Message::Request(req) => {
let response = if req.method == Shutdown::METHOD {
self.shutting_down = true;
Response::new_ok(req.id, ())
} else if self.shutting_down {
Response::new_err(
req.id,
ErrorCode::InvalidRequest as i32,
"the server is shutting down".to_string(),
)
} else if [
Formatting::METHOD,
CodeActionRequest::METHOD,
HoverRequest::METHOD,
GotoDefinition::METHOD,
DocumentSymbolRequest::METHOD,
WorkspaceSymbolRequest::METHOD,
References::METHOD,
CallHierarchyPrepare::METHOD,
CallHierarchyIncomingCalls::METHOD,
CallHierarchyOutgoingCalls::METHOD,
]
.contains(&req.method.as_str())
{
self.check_all(&std::mem::take(stale));
self.document_request(req)
} else {
let what = format!("htl lsp does not answer {}", req.method);
Response::new_err(req.id, ErrorCode::MethodNotFound as i32, what)
};
self.send(response.into());
}
Message::Notification(n) => return self.notification(n, stale),
Message::Response(r) => {
if r.id == RequestId::from(REGISTER_ID.to_string()) {
match r.response_result {
Ok(_) => self.watching = true,
Err(e) => eprintln!(
"htl lsp: the client refused to watch files ({}): {}; the manifests \
are compared at each check instead",
e.code, e.message
),
}
}
}
}
None
}
fn notification(&mut self, n: Notification, stale: &mut Stale) -> Option<ExitCode> {
match n.method.as_str() {
Exit::METHOD => {
return Some(if self.shutting_down {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
});
}
Cancel::METHOD => {}
DidOpenTextDocument::METHOD => {
if let Some(p) = params::<DidOpenTextDocumentParams>(n) {
let d = p.text_document;
self.opened(d.uri, stale, false, Some(d.text));
}
}
DidChangeTextDocument::METHOD => {
if let Some(p) = params::<DidChangeTextDocumentParams>(n) {
self.changed(&p.text_document.uri, p.content_changes);
}
}
DidSaveTextDocument::METHOD => {
if let Some(p) = params::<DidSaveTextDocumentParams>(n) {
self.opened(p.text_document.uri, stale, true, None);
}
}
DidCloseTextDocument::METHOD => {
if let Some(p) = params::<DidCloseTextDocumentParams>(n) {
let uri = p.text_document.uri;
stale.files.retain(|u| *u != uri);
if let Some(doc) = self.open.remove(&uri) {
stale.changed.push(doc.canonical);
self.publish(uri, Vec::new());
}
}
}
DidChangeWatchedFiles::METHOD => {
if let Some(p) = params::<DidChangeWatchedFilesParams>(n) {
let paths: Vec<PathBuf> =
p.changes.iter().filter_map(|c| path_of(&c.uri)).collect();
self.watched(&paths, stale);
}
}
_ => {}
}
None
}
fn changed(&mut self, uri: &Uri, changes: Vec<lsp_types::TextDocumentContentChangeEvent>) {
let Some(last) = changes.into_iter().next_back() else {
return;
};
if let Some(doc) = self.open.get_mut(uri) {
if last.range.is_none() {
doc.buffer = Some(last.text);
doc.unsynced = false;
} else {
eprintln!("htl lsp: an incremental change, which was not asked for; ignored");
doc.buffer = None;
doc.unsynced = true;
}
}
}
fn opened(&mut self, uri: Uri, stale: &mut Stale, saved: bool, text: Option<String>) {
let Some(path) = path_of(&uri) else {
eprintln!("htl lsp: not a file: {}", uri.as_str());
return;
};
let newly_open = !self.open.contains_key(&uri);
if newly_open {
let project = project_key(&path).ok();
self.open.insert(
uri.clone(),
Doc {
canonical: canonical(&path),
path,
project,
deps: HashSet::new(),
buffer: None,
unsynced: false,
checked: None,
},
);
}
if let Some(text) = text
&& let Some(doc) = self.open.get_mut(&uri)
{
doc.buffer = Some(text);
doc.unsynced = false;
}
if let Some(doc) = self.open.get(&uri)
&& (saved || newly_open)
{
stale.changed.push(doc.canonical.clone());
}
if !stale.files.contains(&uri) {
stale.files.push(uri);
}
}
fn watched(&mut self, paths: &[PathBuf], stale: &mut Stale) {
let mut gone: Vec<PathBuf> = Vec::new();
let mut gone_manifest: Vec<PathBuf> = Vec::new();
let mut manifest_dirs: Vec<PathBuf> = Vec::new();
let mut hit: Vec<Uri> = Vec::new();
let mut crates: HashMap<PathBuf, Option<PathBuf>> = HashMap::new();
let mut keys: HashMap<PathBuf, PathBuf> = HashMap::new();
let mut canonical_key = |k: &Path| -> PathBuf {
keys.entry(k.to_path_buf())
.or_insert_with(|| canonical(k))
.clone()
};
for path in paths {
if is_tl(path) {
let path = canonical_dir_of(path);
for (root, index) in &mut self.indices {
if path.starts_with(canonical_key(root)) {
index.event(&path);
}
}
}
let Some(kind) = Watched::of(path) else {
continue;
};
let path = canonical_dir_of(path);
let dir = htl::parent_dir(&path);
if kind == Watched::Manifest {
manifest_dirs.push(dir.clone());
}
for (uri, doc) in &mut self.open {
let key = doc.project.clone();
let reached = match kind {
Watched::Manifest => {
let under = doc.canonical.starts_with(&dir) || doc.path.starts_with(&dir);
if under {
doc.project = None;
}
under
|| key.as_ref().is_some_and(|k| {
let k = canonical_key(k);
k.starts_with(&dir) || dir.starts_with(&k)
})
}
Watched::Types => key
.as_ref()
.is_some_and(|k| path.starts_with(canonical_key(k))),
Watched::Rust => crates
.entry(doc.path.clone())
.or_insert_with(|| htl::dts::find_cargo_package_root(&doc.path))
.as_deref()
.is_some_and(|c| Watched::reads_rust(&path, c)),
};
if reached {
if kind == Watched::Manifest {
gone_manifest.extend(key.clone());
}
gone.extend(key);
if !hit.contains(uri) {
hit.push(uri.clone());
}
}
}
}
self.projects.retain(|k, _| {
let ck = canonical_key(k);
!gone.contains(k)
&& !manifest_dirs
.iter()
.any(|d| ck.starts_with(d) || d.starts_with(&ck))
});
self.indices.retain(|k, _| {
let ck = canonical_key(k);
!gone_manifest.contains(k)
&& !manifest_dirs
.iter()
.any(|d| ck.starts_with(d) || d.starts_with(&ck))
});
for (k, index) in &mut self.indices {
if gone.contains(k) {
index.forget_refs();
}
}
hit.sort();
for uri in hit {
if !stale.files.contains(&uri) {
stale.files.push(uri);
}
}
}
fn check_all(&mut self, stale: &Stale) {
let changed: HashSet<PathBuf> = stale.changed.iter().cloned().collect();
let mut dependents: Vec<Uri> = self
.open
.iter()
.filter(|(u, d)| !stale.files.contains(u) && !d.deps.is_disjoint(&changed))
.map(|(u, _)| u.clone())
.collect();
dependents.sort();
let mut files: Vec<Uri> = stale.files.iter().chain(&dependents).cloned().collect();
let keys: HashSet<PathBuf> = files
.iter()
.filter_map(|u| {
let d = self.open.get(u)?;
d.project.clone().or_else(|| project_key(&d.path).ok())
})
.collect();
for uri in self.drop_moved(keys) {
if !files.contains(&uri) {
files.push(uri);
}
}
for uri in &files {
self.check_one(uri);
}
}
fn drop_moved(&mut self, keys: impl IntoIterator<Item = PathBuf>) -> Vec<Uri> {
let moved: Vec<PathBuf> = keys
.into_iter()
.filter(|k| self.projects.get(k).is_some_and(|p| p.manifests.changed()))
.collect();
if moved.is_empty() {
return Vec::new();
}
for k in &moved {
self.projects.remove(k);
self.indices.remove(k);
}
let mut files: Vec<Uri> = self
.open
.iter()
.filter(|(_, d)| d.project.as_ref().is_some_and(|k| moved.contains(k)))
.map(|(u, _)| u.clone())
.collect();
files.sort();
files
}
fn check_one(&mut self, uri: &Uri) {
let open: Vec<PathBuf> = self.open.values().map(|d| d.path.clone()).collect();
let Some(doc) = self.open.get(uri) else {
return;
};
let (path, known) = (doc.path.clone(), doc.project.clone());
if !path.is_file() {
if let Some(doc) = self.open.get_mut(uri) {
doc.deps.clear();
doc.checked = None;
}
self.publish(uri.clone(), Vec::new());
return;
}
let key = match known {
Some(key) => key,
None => match project_key(&path) {
Ok(key) => {
if let Some(doc) = self.open.get_mut(uri) {
doc.project = Some(key.clone());
}
key
}
Err(_) => htl::parent_dir(&path),
},
};
let stamp = Stamp::of(&path);
let text = read_lossy(&path);
let outcome = self.project(&key, &path).and_then(|p| {
let done = p.check(&path, &open)?;
Ok((
done,
p.scope.lints.selection().clone(),
p.policy,
p.fix_error.clone(),
))
});
let (findings, deps, requires, types) = match outcome {
Ok((done, levels, policy, fix_error)) => {
let mut findings = convert(&path, &text, &done.found, &levels, &policy);
if let Some(e) = fix_error {
findings.push(Finding {
lsp: failure(&anyhow::anyhow!("{e}")),
fix: None,
rule: String::new(),
});
}
(findings, done.deps, done.requires, done.types)
}
Err(e) => (
vec![Finding {
lsp: failure(&e),
fix: None,
rule: String::new(),
}],
Vec::new(),
Vec::new(),
htl::typeindex::TypeIndex::default(),
),
};
let (mut findings, mut requires, mut types) = (findings, requires, types);
if read_lossy(&path) != text {
for f in &mut findings {
f.fix = None;
}
requires.clear();
types = htl::typeindex::TypeIndex::default();
}
let diagnostics = findings.iter().map(|f| f.lsp.clone()).collect();
if let Some(index) = self.indices.get_mut(&key)
&& let Some(stamp) = stamp
&& let Some(decls) = types.declarations()
{
let syms = symbols::flatten(&text, decls);
index.put(
&canonical(&path),
Entry::new(path.clone(), stamp, &text, syms),
deps.iter().map(|d| canonical(d)).collect(),
);
}
if let Some(doc) = self.open.get_mut(uri) {
doc.deps = deps.iter().map(|d| canonical(d)).collect();
doc.checked = Some(Checked {
text,
findings,
requires,
types,
fix_all: None,
});
}
self.publish(uri.clone(), diagnostics);
}
fn document_request(&mut self, req: lsp_server::Request) -> Response {
let id = req.id.clone();
let bad = |e: serde_json::Error| {
Response::new_err(
id.clone(),
ErrorCode::InvalidParams as i32,
format!("malformed {}: {e}", req.method),
)
};
let params = req.params.clone();
match req.method.as_str() {
Formatting::METHOD => {
match serde_json::from_value::<DocumentFormattingParams>(params) {
Ok(p) => Response::new_ok(id.clone(), self.formatting(&p.text_document.uri)),
Err(e) => bad(e),
}
}
HoverRequest::METHOD => match serde_json::from_value::<HoverParams>(params) {
Ok(p) => {
let at = p.text_document_position_params;
Response::new_ok(id.clone(), self.hover(&at.text_document.uri, at.position))
}
Err(e) => bad(e),
},
GotoDefinition::METHOD => {
match serde_json::from_value::<GotoDefinitionParams>(params) {
Ok(p) => {
let at = p.text_document_position_params;
Response::new_ok(
id.clone(),
self.definition(&at.text_document.uri, at.position),
)
}
Err(e) => bad(e),
}
}
DocumentSymbolRequest::METHOD => {
match serde_json::from_value::<DocumentSymbolParams>(params) {
Ok(p) => {
Response::new_ok(id.clone(), self.document_symbols(&p.text_document.uri))
}
Err(e) => bad(e),
}
}
References::METHOD => match serde_json::from_value::<ReferenceParams>(params) {
Ok(p) => {
let at = p.text_document_position;
Response::new_ok(
id.clone(),
self.references(
&at.text_document.uri,
at.position,
p.context.include_declaration,
),
)
}
Err(e) => bad(e),
},
CallHierarchyPrepare::METHOD => {
match serde_json::from_value::<CallHierarchyPrepareParams>(params) {
Ok(p) => {
let at = p.text_document_position_params;
Response::new_ok(
id.clone(),
self.prepare_calls(&at.text_document.uri, at.position),
)
}
Err(e) => bad(e),
}
}
CallHierarchyIncomingCalls::METHOD => {
match serde_json::from_value::<CallHierarchyIncomingCallsParams>(params) {
Ok(p) => Response::new_ok(id.clone(), self.incoming_calls(&p.item)),
Err(e) => bad(e),
}
}
CallHierarchyOutgoingCalls::METHOD => {
match serde_json::from_value::<CallHierarchyOutgoingCallsParams>(params) {
Ok(p) => Response::new_ok(id.clone(), self.outgoing_calls(&p.item)),
Err(e) => bad(e),
}
}
WorkspaceSymbolRequest::METHOD => {
match serde_json::from_value::<WorkspaceSymbolParams>(params) {
Ok(p) => Response::new_ok(id.clone(), self.workspace_symbols(&p.query)),
Err(e) => bad(e),
}
}
_ => match serde_json::from_value::<CodeActionParams>(params) {
Ok(p) => Response::new_ok(id.clone(), self.code_actions(&p)),
Err(e) => bad(e),
},
}
}
fn checked_buffer(&self, uri: &Uri) -> Option<&Checked> {
let doc = self.open.get(uri).filter(|d| !d.unsynced)?;
let checked = doc.checked.as_ref()?;
let buffer = match &doc.buffer {
Some(buffer) => buffer.as_str(),
None => &read_lossy(&doc.path),
};
(buffer == checked.text).then_some(checked)
}
fn hover(&self, uri: &Uri, at: Position) -> Option<Hover> {
let checked = self.checked_buffer(uri)?;
let lines = lines_of(&checked.text);
let (line, col) = byte_position(&lines, at)?;
let found = checked.types.at(&checked.text, line, col)?;
let ty = found.ty?;
Some(Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: format!("```teal\n{ty}\n```"),
}),
range: Some(range_at(&lines, found.line, found.col)),
})
}
fn definition(&self, uri: &Uri, at: Position) -> Option<GotoDefinitionResponse> {
let checked = self.checked_buffer(uri)?;
let lines = lines_of(&checked.text);
let (line, col) = byte_position(&lines, at)?;
let here = path_of(uri)?;
if let Some(site) = required_at(&lines, &checked.requires, line, col) {
let file = site.path.as_ref()?;
let target = location_of(&here, uri, file, &checked.text, None)?;
return Some(GotoDefinitionResponse::Scalar(target));
}
let place = checked.types.at(&checked.text, line, col)?.declared?;
let target = location_of(
&here,
uri,
&place.file,
&checked.text,
Some((place.line, place.col)),
)?;
Some(GotoDefinitionResponse::Scalar(target))
}
fn document_symbols(&self, uri: &Uri) -> Option<DocumentSymbolResponse> {
let checked = self.checked_buffer(uri)?;
let decls = checked.types.declarations()?;
let lines = lines_of(&checked.text);
Some(DocumentSymbolResponse::Nested(
decls.iter().map(|d| symbol_of(&lines, d)).collect(),
))
}
fn workspace_symbols(&mut self, query: &str) -> Option<WorkspaceSymbolResponse> {
let mut docs: Vec<(&Uri, &Doc)> = self.open.iter().collect();
docs.sort_by(|a, b| a.0.cmp(b.0));
let mut asked: Vec<(PathBuf, PathBuf)> = Vec::new();
for (_, d) in docs {
let key = d
.project
.clone()
.or_else(|| project_key(&d.path).ok())
.unwrap_or_else(|| htl::parent_dir(&d.path));
if !asked.iter().any(|(k, _)| *k == key) {
asked.push((key, d.path.clone()));
}
}
if asked.is_empty() {
for dir in &self.workspace {
match htl::model::Project::find_root(dir) {
Ok(Some(found)) => {
if !asked.iter().any(|(k, _)| *k == found.root) {
asked.push((found.root, dir.clone()));
}
}
Ok(None) => {}
Err(e) => eprintln!("htl lsp: no symbols for {}: {e:#}", dir.display()),
}
}
if asked.is_empty() {
eprintln!(
"htl lsp: no symbols to search: no file is open and no workspace folder \
is in a project"
);
return None;
}
}
for other in self.drop_moved(asked.iter().map(|(k, _)| k.clone()).collect::<Vec<_>>()) {
self.check_one(&other);
}
let mut built: Vec<&PathBuf> = Vec::new();
for (key, file) in &asked {
match self.refresh_symbols(key, file) {
Ok(()) => built.push(key),
Err(e) => eprintln!("htl lsp: no symbols for {}: {e:#}", key.display()),
}
}
if built.is_empty() {
return None;
}
let mut out = self.symbol_answer(&built, query);
if !out.1.is_empty() {
let mut again: Vec<&PathBuf> = Vec::new();
for key in &built {
if let Some(index) = self.indices.get_mut(*key)
&& index.entries().any(|e| out.1.contains(&e.path))
{
index.relist();
again.push(*key);
}
}
for key in again {
let file = &asked.iter().find(|(k, _)| k == key).expect("asked").1;
if let Err(e) = self.refresh_symbols(key, file) {
eprintln!("htl lsp: no symbols for {}: {e:#}", key.display());
}
}
out = self.symbol_answer(&built, query);
}
Some(WorkspaceSymbolResponse::Flat(out.0))
}
fn symbol_answer(
&self,
built: &[&PathBuf],
query: &str,
) -> (Vec<SymbolInformation>, Vec<PathBuf>) {
let (hits, total) = symbols::search(
built
.iter()
.filter_map(|k| self.indices.get(*k))
.flat_map(SymbolIndex::entries),
query,
);
if total > hits.len() {
eprintln!(
"htl lsp: {total} symbols match {query:?}; the first {} are answered",
hits.len()
);
}
let mut gone: Vec<PathBuf> = Vec::new();
let mut uri: Option<(&Path, Option<Uri>)> = None;
let mut out = Vec::with_capacity(hits.len());
for h in hits {
if uri.as_ref().is_none_or(|(p, _)| *p != h.path) {
let target = if h.path.is_file() {
std::path::absolute(h.path).ok().and_then(|p| uri_of(&p))
} else {
if !gone.iter().any(|g| g == h.path) {
gone.push(h.path.to_path_buf());
}
None
};
uri = Some((h.path, target));
}
let Some((_, Some(target))) = &uri else {
continue;
};
#[allow(deprecated)]
out.push(SymbolInformation {
name: h.sym.name.clone(),
kind: h.sym.kind,
tags: None,
deprecated: None,
location: Location {
uri: target.clone(),
range: h.sym.range,
},
container_name: h.sym.container.clone(),
});
}
(out, gone)
}
fn refresh_symbols(&mut self, key: &Path, file: &Path) -> Result<()> {
self.project(key, file)?;
let mut index = self.indices.remove(key).unwrap_or_default();
let project = &self.projects[key];
let open: Vec<&Doc> = self.open.values().collect();
let own: Vec<PathBuf> = open
.iter()
.filter(|d| d.project.as_deref() == Some(key) || htl::parent_dir(&d.path) == key)
.map(|d| d.path.clone())
.collect();
let sweep = !self.watching || project.model.is_none();
let result = index.refresh(
sweep,
|| project.source_files(key, &own),
&mut |canonical, path, text| project.symbols_of(&open, canonical, path, text),
);
self.indices.insert(key.to_path_buf(), index);
result
}
fn project_ready(&mut self, uri: &Uri, what: &str) -> Option<(PathBuf, PathBuf)> {
let here = path_of(uri)?;
let key = self
.open
.get(uri)
.and_then(|d| d.project.clone())
.or_else(|| project_key(&here).ok())
.unwrap_or_else(|| htl::parent_dir(&here));
for other in self.drop_moved([key.clone()]) {
self.check_one(&other);
}
if let Err(e) = self.refresh_symbols(&key, &here) {
eprintln!("htl lsp: no {what} for {}: {e:#}", key.display());
return None;
}
if !self.watching
&& self.projects.get(&key).is_some_and(|p| p.model.is_some())
&& self
.indices
.get_mut(&key)
.is_some_and(|i| i.inputs_changed(project_inputs(&key)))
{
self.projects.remove(&key);
if let Some(index) = self.indices.get_mut(&key) {
index.forget_refs();
}
let mut files: Vec<Uri> = self
.open
.iter()
.filter(|(_, d)| d.project.as_ref() == Some(&key))
.map(|(u, _)| u.clone())
.collect();
files.sort();
for other in files {
self.check_one(&other);
}
}
let held = self
.indices
.get(&key)
.is_some_and(|i| i.holds(&canonical(&here)));
if !held {
return None;
}
if let Some(index) = self.indices.get_mut(&key) {
index.first_refs();
}
if self
.indices
.get(&key)
.is_some_and(|i| i.distrusts(&canonical(&here)))
{
self.check_one(uri);
}
Some((key, here))
}
fn references(
&mut self,
uri: &Uri,
at: Position,
include_declaration: bool,
) -> Option<Vec<Location>> {
let (key, here) = self.project_ready(uri, "references")?;
let (text, spelled, target) = {
let checked = self.checked_buffer(uri)?;
let lines = lines_of(&checked.text);
let (line, col) = byte_position(&lines, at)?;
let spelled = checked.types.at(&checked.text, line, col)?.declared?;
let target = refs::Place::of(&spelled)?;
(checked.text.clone(), spelled, target)
};
let here_canonical = canonical(&here);
let mut round = 0;
loop {
self.refresh_refs(&key);
let index = self.indices.get(&key)?;
let class = index.same_as(&target);
let mut hits: Vec<(&PathBuf, &symbols::Entry, &refs::Ref)> = Vec::new();
for (canonical, e) in index.iter() {
for r in e.refs.iter().flat_map(|f| &f.refs) {
if class.contains(&r.declared) {
hits.push((canonical, e, r));
}
}
}
let own: HashSet<usize> = class
.iter()
.filter_map(|p| {
hits.iter()
.enumerate()
.filter(|(_, (c, _, r))| {
**c == *p.file && r.declared == *p && r.line == p.line
})
.min_by_key(|(_, (_, _, r))| r.col)
.map(|(i, _)| i)
})
.collect();
let mut out: Vec<Location> = Vec::new();
if include_declaration
&& own.is_empty()
&& let Some(point) = location_of(
&here,
uri,
&spelled.file,
&text,
Some((spelled.line, spelled.col)),
)
{
out.push(point);
}
let mut gone = false;
for (i, (canonical, e, r)) in hits.iter().enumerate() {
if own.contains(&i) && !include_declaration {
continue;
}
if !e.path.is_file() {
gone = true;
continue;
}
let target_uri = if **canonical == here_canonical {
Some(uri.clone())
} else {
std::path::absolute(&e.path).ok().and_then(|p| uri_of(&p))
};
if let Some(target_uri) = target_uri {
out.push(Location {
uri: target_uri,
range: r.range,
});
}
}
if gone && round == 0 {
round += 1;
if let Some(index) = self.indices.get_mut(&key) {
index.relist();
}
if let Err(e) = self.refresh_symbols(&key, &here) {
eprintln!("htl lsp: no references for {}: {e:#}", key.display());
return None;
}
continue;
}
return Some(out);
}
}
fn prepare_calls(&mut self, uri: &Uri, at: Position) -> Option<Vec<CallHierarchyItem>> {
let (key, here) = self.project_ready(uri, "call hierarchy")?;
let here_canonical = canonical(&here);
let (target, mine) = {
let checked = self.checked_buffer(uri)?;
let lines = lines_of(&checked.text);
let (line, col) = byte_position(&lines, at)?;
let spelled = checked.types.at(&checked.text, line, col)?.declared?;
let target = refs::Place::of(&spelled)?;
let mine = (*target.file == *here_canonical).then(|| {
checked
.types
.declarations()
.map(|d| symbols::flatten(&checked.text, d))
.unwrap_or_default()
});
(target, mine)
};
let place = (target.line, target.col);
if let Some(syms) = mine {
let sym = &syms[symbols::find(&syms, place)?];
return sym
.is_function()
.then(|| vec![calls::item(uri.clone(), sym)]);
}
self.refresh_refs(&key);
let e = self.indices.get(&key)?.entry(&target.file)?;
let syms = &e.refs.as_ref()?.syms;
let sym = &syms[symbols::find(syms, place)?];
if !sym.is_function() || !e.path.is_file() {
return None;
}
let uri = std::path::absolute(&e.path).ok().and_then(|p| uri_of(&p))?;
Some(vec![calls::item(uri, sym)])
}
fn incoming_calls(
&mut self,
item: &CallHierarchyItem,
) -> Option<Vec<lsp_types::CallHierarchyIncomingCall>> {
let (key, canonical_here) = self.calls_ready(item)?;
let data = item.data.clone().unwrap_or_default();
let root = canonical(&key);
self.calls_answer(&key, &item.uri, |index| {
let ctx = calls::Ctx {
root: &root,
here: &canonical_here,
uri: &item.uri,
};
let (_, fr, which) = calls::resolve(index, &canonical_here, &data)?;
let Some(i) = which else {
return Some((Vec::new(), false));
};
Some(calls::incoming(
index,
&ctx,
&calls::places(&canonical_here, &fr.syms[i]),
))
})
}
fn outgoing_calls(
&mut self,
item: &CallHierarchyItem,
) -> Option<Vec<lsp_types::CallHierarchyOutgoingCall>> {
let (key, canonical_here) = self.calls_ready(item)?;
let data = item.data.clone().unwrap_or_default();
let root = canonical(&key);
self.calls_answer(&key, &item.uri, |index| {
let ctx = calls::Ctx {
root: &root,
here: &canonical_here,
uri: &item.uri,
};
let (_, fr, which) = calls::resolve(index, &canonical_here, &data)?;
Some(calls::outgoing(index, &ctx, fr, which))
})
}
fn calls_ready(&mut self, item: &CallHierarchyItem) -> Option<(PathBuf, PathBuf)> {
let canonical_here = canonical(&path_of(&item.uri)?);
let open = self
.open
.iter()
.find(|(_, d)| d.canonical == canonical_here)
.map(|(u, _)| u.clone());
let uri = open.clone().unwrap_or_else(|| item.uri.clone());
let (key, _) = self.project_ready(&uri, "call hierarchy")?;
if let Some(open) = open {
self.checked_buffer(&open)?;
}
Some((key, canonical_here))
}
fn calls_answer<T>(
&mut self,
key: &Path,
uri: &Uri,
answer: impl Fn(&SymbolIndex) -> Option<(T, bool)>,
) -> Option<T> {
let here = path_of(uri)?;
let mut round = 0;
loop {
self.refresh_refs(key);
let (out, gone) = match answer(self.indices.get(key)?) {
Some(found) => found,
None => {
eprintln!(
"htl lsp: no call hierarchy for an item of {}: it declares no such function",
here.display()
);
return None;
}
};
if gone && round == 0 {
round += 1;
if let Some(index) = self.indices.get_mut(key) {
index.relist();
}
if let Err(e) = self.refresh_symbols(key, &here) {
eprintln!("htl lsp: no call hierarchy for {}: {e:#}", key.display());
return None;
}
continue;
}
return Some(out);
}
}
fn refresh_refs(&mut self, key: &Path) {
let Some(mut index) = self.indices.remove(key) else {
return;
};
if let Some(project) = self.projects.get(key) {
for p in index.pending() {
let text = read_lossy(&p.path);
let hash = symbols::hash_of(&text);
let open = self.open.values().find(|d| d.canonical == p.canonical);
let from_check = open
.filter(|_| !p.distrust)
.and_then(|d| d.checked.as_ref().map(|c| (d, c)))
.filter(|(_, c)| c.text == text)
.map(|(d, c)| refs::collect(&text, &c.types, d.deps.iter().cloned()));
let made = from_check.or_else(|| match project.indexed(&p.path) {
Ok(_) if read_lossy(&p.path) != text => None,
Ok(c) => Some(refs::collect(
&text,
&c.types,
c.deps.iter().map(|d| canonical(d)),
)),
Err(e) => {
eprintln!("htl lsp: no references from {}: {e:#}", p.path.display());
Some(refs::FileRefs::default())
}
});
if let Some(made) = made {
index.fill(&p.canonical, hash, made);
}
}
}
self.indices.insert(key.to_path_buf(), index);
}
fn formatting(&mut self, uri: &Uri) -> Option<Vec<TextEdit>> {
let path = path_of(uri)?;
let key = project_key(&path).unwrap_or_else(|_| htl::parent_dir(&path));
if self.open.get(uri).is_some_and(|d| d.unsynced) {
eprintln!(
"htl lsp: not formatting {}: its buffer is not known",
path.display()
);
return None;
}
let buffer = self.open.get(uri).and_then(|d| d.buffer.clone());
for other in self.drop_moved([key.clone()]) {
self.check_one(&other);
}
let result = (|| {
let before = match buffer {
Some(text) => text,
None => std::fs::read_to_string(&path)
.with_context(|| format!("reading {}", path.display()))?,
};
let after = self.project(&key, &path)?.format(&path, &before)?;
Ok::<_, anyhow::Error>((before, after))
})();
match result {
Ok((before, after)) => Some(minimal_edit(&before, &after).into_iter().collect()),
Err(e) => {
eprintln!("htl lsp: not formatting {}: {e:#}", path.display());
None
}
}
}
fn code_actions(&mut self, p: &CodeActionParams) -> Vec<CodeActionOrCommand> {
let uri = &p.text_document.uri;
let wanted = |kind: &CodeActionKind| match &p.context.only {
None => true,
Some(only) => only.iter().any(|o| kind_within(kind, o)),
};
let quick = wanted(&CodeActionKind::QUICKFIX);
let all = match &p.context.only {
Some(only) => only.iter().any(|o| kind_within(&fix_all_kind(), o)),
None => p.context.trigger_kind == Some(CodeActionTriggerKind::INVOKED),
};
let Some(doc) = self.open.get(uri).filter(|d| !d.unsynced) else {
return Vec::new();
};
let (Some(checked), Some(key)) = (&doc.checked, doc.project.clone()) else {
return Vec::new();
};
let buffer = doc.buffer.clone().unwrap_or_else(|| read_lossy(&doc.path));
if buffer != checked.text {
return Vec::new();
}
let Some(project) = self.projects.get(&key) else {
return Vec::new();
};
if project.fix_error.is_some() {
return Vec::new();
}
let mut actions = Vec::new();
if quick {
for f in &checked.findings {
let Some(fix) = &f.fix else { continue };
if !overlaps(&f.lsp.range, &p.range) {
continue;
}
let class = project.fix.effective(&f.rule, fix);
if class == htl::Applicability::Suggest {
continue;
}
let Some(after) = htl::fix::apply_fix(&buffer, fix) else {
continue;
};
let Some(edit) = minimal_edit(&buffer, &after) else {
continue;
};
let applied = project.fix.disposition(&f.rule, fix)
== htl::fix::Disposition::Apply(htl::Applicability::Safe);
let unsafe_ = class == htl::Applicability::Unsafe;
actions.push(CodeActionOrCommand::CodeAction(CodeAction {
title: format!("Fix {}{}", f.rule, if unsafe_ { " (unsafe)" } else { "" }),
kind: Some(CodeActionKind::QUICKFIX),
diagnostics: Some(vec![f.lsp.clone()]),
edit: Some(edit_of(uri, vec![edit])),
is_preferred: Some(applied),
..Default::default()
}));
}
}
let applicable = checked.findings.iter().any(|f| {
f.fix.as_ref().is_some_and(|fix| {
matches!(
project.fix.disposition(&f.rule, fix),
htl::fix::Disposition::Apply(_)
)
})
});
if all && applicable && read_lossy(&doc.path) == checked.text {
let path = doc.path.clone();
let fixed = match checked.fix_all.clone() {
Some(known) => Some(known),
None => match project.fixed(&path) {
Ok(known) => {
if let Some(doc) = self.open.get_mut(uri)
&& let Some(checked) = &mut doc.checked
{
checked.fix_all = Some(known.clone());
}
Some(known)
}
Err(e) => {
eprintln!("htl lsp: no fix-all for {}: {e:#}", path.display());
None
}
},
}
.flatten();
if let Some(edit) = fixed.and_then(|text| minimal_edit(&buffer, &text)) {
actions.push(CodeActionOrCommand::CodeAction(CodeAction {
title: "Fix all htl problems that are safe to fix".to_string(),
kind: Some(fix_all_kind()),
edit: Some(edit_of(uri, vec![edit])),
..Default::default()
}));
}
}
actions
}
fn project(&mut self, key: &Path, file: &Path) -> Result<&Project> {
if !self.projects.contains_key(key) {
let loaded = Project::load(file, key)?;
self.projects.insert(key.to_path_buf(), loaded);
}
Ok(&self.projects[key])
}
fn publish(&self, uri: Uri, diagnostics: Vec<lsp_types::Diagnostic>) {
let params = PublishDiagnosticsParams {
uri,
diagnostics,
version: None,
};
self.send(Notification::new(PublishDiagnostics::METHOD.to_string(), params).into());
}
fn send(&self, message: Message) {
let _ = self.connection.sender.send(message);
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Watched {
Manifest,
Types,
Rust,
}
impl Watched {
fn of(path: &Path) -> Option<Self> {
let name = path.file_name()?.to_str()?;
if Manifests::names().contains(&name) {
Some(Self::Manifest)
} else if name.ends_with(".d.tl") && path.components().any(|c| c.as_os_str() == "types") {
Some(Self::Types)
} else if name == "Cargo.toml" || name.ends_with(".rs") {
Some(Self::Rust)
} else {
None
}
}
fn reads_rust(path: &Path, root: &Path) -> bool {
let Ok(rel) = path.strip_prefix(root) else {
return false;
};
let mut parts = rel.components().map(|c| c.as_os_str());
matches!(
(parts.next().and_then(|p| p.to_str()), parts.next()),
(Some("Cargo.toml"), None) | (Some("src" | "examples" | "tests" | "benches"), Some(_))
)
}
}
fn project_inputs(root: &Path) -> u64 {
fn walk(dir: &Path, root: &Path, out: &mut Vec<(PathBuf, Option<std::time::SystemTime>, u64)>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for e in entries.flatten() {
let path = e.path();
if path.is_dir() {
if !htl::is_skipped_dir(&path, &[]) {
walk(&path, root, out);
}
continue;
}
let read = match Watched::of(&path) {
Some(Watched::Types) => true,
Some(Watched::Rust) => {
path.file_name().is_some_and(|n| n == "Cargo.toml")
|| path.strip_prefix(root).is_ok_and(|rel| {
rel.components().any(|c| {
["src", "examples", "tests", "benches"]
.iter()
.any(|d| c.as_os_str() == *d)
})
})
}
_ => false,
};
if read && let Ok(m) = e.metadata() {
out.push((path, m.modified().ok(), m.len()));
}
}
}
let mut seen = Vec::new();
walk(root, root, &mut seen);
seen.sort();
let mut h = DefaultHasher::new();
seen.hash(&mut h);
h.finish()
}
fn byte_position(lines: &[&str], at: Position) -> Option<(usize, usize)> {
let text = lines.get(at.line as usize)?;
let mut units = 0u32;
let mut byte = text.len();
for (i, c) in text.char_indices() {
if units >= at.character {
byte = i;
break;
}
units += c.len_utf16() as u32;
}
Some((at.line as usize + 1, byte + 1))
}
fn required_at<'a>(
lines: &[&str],
sites: &'a [htl::RequireSite],
line: usize,
col: usize,
) -> Option<&'a htl::RequireSite> {
let text = lines.get(line - 1)?;
sites.iter().filter(|s| s.line == line).find(|s| {
let from = (s.col.saturating_sub(1)).min(text.len());
let Some(open) = text.get(from..).and_then(|t| t.find(['"', '\''])) else {
return false;
};
let open = from + open;
let quote = text.as_bytes()[open];
let close = text.as_bytes()[open + 1..]
.iter()
.position(|c| *c == quote)
.map_or(text.len(), |p| open + 1 + p + 1);
(open + 1..=close + 1).contains(&col)
})
}
fn location_of(
here: &Path,
uri: &Uri,
file: &Path,
text: &str,
place: Option<(usize, usize)>,
) -> Option<Location> {
let mut path = PathBuf::new();
for part in std::env::current_dir()
.unwrap_or_default()
.join(file)
.components()
{
if part != std::path::Component::CurDir {
path.push(part);
}
}
let target = if path == here {
uri.clone()
} else if path.is_file() {
uri_of(&path)?
} else {
return None;
};
let start = match place {
None => Position::new(0, 0),
Some((line, col)) => {
let theirs;
let theirs_text = if path == here {
text
} else {
theirs = read_lossy(&path);
&theirs
};
range_at(&lines_of(theirs_text), line, col).start
}
};
Some(Location {
uri: target,
range: Range::new(start, start),
})
}
fn params<P: serde::de::DeserializeOwned>(n: Notification) -> Option<P> {
match serde_json::from_value(n.params) {
Ok(p) => Some(p),
Err(e) => {
eprintln!("htl lsp: malformed {}: {e}", n.method);
None
}
}
}
fn is_tl(path: &Path) -> bool {
path.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.ends_with(".tl") && !n.ends_with(".d.tl"))
}
fn workspace_of(params: &serde_json::Value) -> Vec<PathBuf> {
let uri_path = |v: &serde_json::Value| {
v.as_str()
.and_then(|u| Uri::from_str(u).ok())
.and_then(|u| path_of(&u))
};
let folders: Vec<PathBuf> = params
.get("workspaceFolders")
.and_then(serde_json::Value::as_array)
.map(|a| a.iter().filter_map(|f| uri_path(&f["uri"])).collect())
.unwrap_or_default();
if !folders.is_empty() {
return folders;
}
if let Some(root) = uri_path(¶ms["rootUri"]) {
return vec![root];
}
params["rootPath"]
.as_str()
.map(|p| vec![PathBuf::from(p)])
.unwrap_or_default()
}
fn path_of(uri: &Uri) -> Option<PathBuf> {
url::Url::parse(uri.as_str()).ok()?.to_file_path().ok()
}
fn uri_of(path: &Path) -> Option<Uri> {
let url = url::Url::from_file_path(path).ok()?;
Uri::from_str(url.as_str()).ok()
}
fn project_key(file: &Path) -> Result<PathBuf> {
Ok(match htl::model::Project::find_root(file)? {
Some(found) => found.root,
None => htl::parent_dir(file),
})
}
fn canonical(p: &Path) -> PathBuf {
std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
}
fn canonical_dir_of(p: &Path) -> PathBuf {
match (p.parent(), p.file_name()) {
(Some(dir), Some(name)) => canonical(dir).join(name),
_ => p.to_path_buf(),
}
}
fn failure(e: &anyhow::Error) -> lsp_types::Diagnostic {
lsp_types::Diagnostic {
range: Range::default(),
severity: Some(DiagnosticSeverity::ERROR),
source: Some("htl".to_string()),
message: format!("{e:#}"),
..Default::default()
}
}
fn convert(
file: &Path,
text: &str,
found: &[htl::Diagnostic],
levels: &htl::lint::Selection,
policy: &htl::verdict::Policy,
) -> Vec<Finding> {
let lines = lines_of(text);
let cwd = std::env::current_dir().unwrap_or_default();
let file_canonical = canonical(file);
found
.iter()
.map(|d| {
let at = cwd.join(&d.file);
let own = d.file.is_empty() || at == file || canonical(&at) == file_canonical;
let (range, related) = if own {
(range_at(&lines, d.line, d.col), None)
} else {
let related = uri_of(&at).map(|uri| {
let theirs = read_lossy(&at);
let start = range_at(&lines_of(&theirs), d.line, d.col).start;
vec![DiagnosticRelatedInformation {
location: Location {
uri,
range: Range::new(start, start),
},
message: d.message.clone(),
}]
});
(Range::default(), related)
};
let message = if own {
d.message.clone()
} else {
format!("{}:{}:{}: {}", d.file, d.line, d.col, d.message)
};
let lsp = lsp_types::Diagnostic {
range,
severity: Some(severity(d, levels, policy)),
code: d.rule.clone().map(NumberOrString::String),
source: Some("htl".to_string()),
message,
related_information: related,
..Default::default()
};
let fix = d
.fix
.clone()
.filter(|_| own && d.severity != htl::Severity::Warning);
Finding {
lsp,
fix,
rule: htl::fix::rule_of(d),
}
})
.collect()
}
fn read_lossy(file: &Path) -> String {
std::fs::read(file)
.map(|b| String::from_utf8_lossy(&b).into_owned())
.unwrap_or_default()
}
fn lines_of(text: &str) -> Vec<&str> {
text.split('\n')
.map(|l| l.strip_suffix('\r').unwrap_or(l))
.collect()
}
fn fix_all_kind() -> CodeActionKind {
CodeActionKind::new("source.fixAll.htl")
}
fn kind_within(kind: &CodeActionKind, only: &CodeActionKind) -> bool {
let (kind, only) = (kind.as_str(), only.as_str());
kind == only || kind.strip_prefix(only).is_some_and(|r| r.starts_with('.'))
}
fn overlaps(a: &Range, b: &Range) -> bool {
a.start <= b.end && b.start <= a.end
}
fn edit_of(uri: &Uri, edits: Vec<TextEdit>) -> WorkspaceEdit {
WorkspaceEdit {
changes: Some(HashMap::from([(uri.clone(), edits)])),
..Default::default()
}
}
fn minimal_edit(before: &str, after: &str) -> Option<TextEdit> {
if before == after {
return None;
}
let mut start = before
.bytes()
.zip(after.bytes())
.take_while(|(a, b)| a == b)
.count();
while !before.is_char_boundary(start) || !after.is_char_boundary(start) {
start -= 1;
}
if start > 0
&& before.as_bytes()[start - 1] == b'\r'
&& before.as_bytes().get(start) == Some(&b'\n')
{
start -= 1;
}
let (rb, ra) = (&before.as_bytes()[start..], &after.as_bytes()[start..]);
let mut shared = rb
.iter()
.rev()
.zip(ra.iter().rev())
.take_while(|(a, b)| a == b)
.count();
while shared > 0
&& (!before.is_char_boundary(before.len() - shared)
|| !after.is_char_boundary(after.len() - shared)
|| (before.as_bytes()[before.len() - shared] == b'\n'
&& before.len() - shared > start
&& before.as_bytes()[before.len() - shared - 1] == b'\r'))
{
shared -= 1;
}
let (end_before, end_after) = (before.len() - shared, after.len() - shared);
let at = |upto: usize| {
let b = before.as_bytes();
let (mut line, mut line_start) = (0u32, 0usize);
for i in 0..upto {
if b[i] == b'\n' || (b[i] == b'\r' && b.get(i + 1) != Some(&b'\n')) {
line += 1;
line_start = i + 1;
}
}
let col = before[line_start..upto].encode_utf16().count();
Position::new(line, col as u32)
};
Some(TextEdit::new(
Range::new(at(start), at(end_before)),
after[start..end_after].to_string(),
))
}
fn severity(
d: &htl::Diagnostic,
levels: &htl::lint::Selection,
policy: &htl::verdict::Policy,
) -> DiagnosticSeverity {
match d.severity {
htl::Severity::Error => DiagnosticSeverity::ERROR,
htl::Severity::Warning | htl::Severity::Lint
if !policy.capped && (policy.strict || htl::verdict::is_denied(d, levels)) =>
{
DiagnosticSeverity::ERROR
}
htl::Severity::Warning | htl::Severity::Lint => DiagnosticSeverity::WARNING,
}
}
fn position_at(lines: &[&str], line: usize, col: usize) -> Position {
let (n, text, at) = clamped(lines, line, col);
Position::new(n as u32, text[..at].encode_utf16().count() as u32)
}
fn clamped<'a>(lines: &[&'a str], line: usize, col: usize) -> (usize, &'a str, usize) {
let n = line.saturating_sub(1).min(lines.len().saturating_sub(1));
let text = lines.get(n).copied().unwrap_or("");
let mut at = col.saturating_sub(1).min(text.len());
while !text.is_char_boundary(at) {
at -= 1;
}
(n, text, at)
}
fn symbol_of(lines: &[&str], d: &htl::typeindex::Decl) -> DocumentSymbol {
let start = position_at(lines, d.line, d.col);
let end = position_at(lines, d.end_line, d.end_col.unwrap_or(usize::MAX));
let name_start = position_at(lines, d.name_line, d.name_col);
let name_end = position_at(
lines,
d.name_line,
d.name_col.saturating_add(d.short_name().len()),
);
let children: Vec<DocumentSymbol> = d.children.iter().map(|c| symbol_of(lines, c)).collect();
#[allow(deprecated)]
DocumentSymbol {
name: d.name.clone(),
detail: d.detail.clone(),
kind: symbols::kind_of(d.kind),
tags: None,
deprecated: None,
range: Range::new(start.min(name_start), end.max(name_end)),
selection_range: Range::new(name_start, name_end),
children: (!children.is_empty()).then_some(children),
}
}
fn range_at(lines: &[&str], line: usize, col: usize) -> Range {
if line == 0 {
return Range::default();
}
let (n, text, start) = clamped(lines, line, col);
let word = |c: char| c.is_alphanumeric() || c == '_';
let mut end = start;
let mut rest = text[start..].chars();
if let Some(first) = rest.next() {
end += first.len_utf8();
if word(first) {
end += rest
.take_while(|c| word(*c))
.map(char::len_utf8)
.sum::<usize>();
}
}
let units = |upto: usize| text[..upto].encode_utf16().count() as u32;
Range::new(
Position::new(n as u32, units(start)),
Position::new(n as u32, units(end)),
)
}
#[cfg(test)]
#[allow(dead_code)]
#[path = "../../htl-core/tests/common/mod.rs"]
mod core_common;
#[cfg(test)]
mod tests {
use super::*;
use lsp_server::Request;
use std::time::Duration;
#[test]
fn a_column_in_bytes_becomes_a_column_in_utf16_units() {
let lines = ["local s = \"é😀\"; local n"];
let byte = lines[0].rfind('n').unwrap();
assert_eq!(byte, 26);
let r = range_at(&lines, 1, byte + 1);
assert_eq!(r.start, Position::new(0, 23));
assert_eq!(r.end, Position::new(0, 24));
}
#[test]
fn a_range_covers_the_identifier_that_starts_at_the_point() {
let lines = ["local name_1 = 3", ""];
let r = range_at(&lines, 1, 7);
assert_eq!(
(r.start, r.end),
(Position::new(0, 6), Position::new(0, 12))
);
let r = range_at(&lines, 1, 13);
assert_eq!(
(r.start, r.end),
(Position::new(0, 12), Position::new(0, 13))
);
let r = range_at(&lines, 2, 1);
assert_eq!((r.start, r.end), (Position::new(1, 0), Position::new(1, 0)));
assert_eq!(range_at(&lines, 0, 0), Range::default());
}
#[test]
fn a_column_past_the_line_or_inside_a_character_is_clamped() {
let lines = ["é"];
let r = range_at(&lines, 1, 2);
assert_eq!((r.start, r.end), (Position::new(0, 0), Position::new(0, 1)));
let r = range_at(&lines, 1, 40);
assert_eq!((r.start, r.end), (Position::new(0, 1), Position::new(0, 1)));
}
#[test]
fn a_path_and_its_uri_name_each_other() {
let path = Path::new("/work/my project/é.tl");
let uri = uri_of(path).unwrap();
assert_eq!(uri.as_str(), "file:///work/my%20project/%C3%A9.tl");
assert_eq!(path_of(&uri).as_deref(), Some(path));
assert_eq!(
path_of(&Uri::from_str("untitled:Untitled-1").unwrap()),
None
);
}
fn applied(before: &str, edit: &TextEdit) -> String {
let at = |p: Position| {
let line_start: usize = before
.split('\n')
.take(p.line as usize)
.map(|l| l.len() + 1)
.sum();
let mut units = 0;
let mut off = line_start;
for c in before[line_start..].chars() {
if units >= p.character || c == '\n' {
break;
}
units += c.len_utf16() as u32;
off += c.len_utf8();
}
off
};
let mut out = before.to_string();
out.replace_range(at(edit.range.start)..at(edit.range.end), &edit.new_text);
out
}
#[test]
fn an_edit_covers_only_the_span_that_differs() {
assert!(minimal_edit("a\nb\n", "a\nb\n").is_none());
let e = minimal_edit("a\nbcd\ne\n", "a\nbXd\ne\n").unwrap();
assert_eq!(e.new_text, "X");
assert_eq!(
(e.range.start, e.range.end),
(Position::new(1, 1), Position::new(1, 2))
);
let e = minimal_edit("ab", "aXb").unwrap();
assert_eq!(
(e.range.start, e.range.end),
(Position::new(0, 1), Position::new(0, 1))
);
let e = minimal_edit("aXb", "ab").unwrap();
assert_eq!(
(e.new_text.as_str(), e.range.end),
("", Position::new(0, 2))
);
let e = minimal_edit("aaa", "aa").unwrap();
assert_eq!(applied("aaa", &e), "aa");
}
#[test]
fn an_edit_is_in_utf16_and_not_in_the_middle_of_a_character_or_a_crlf() {
for (a, b) in [
("x \u{e9}y", "x \u{e8}y"),
("\u{1F600}a", "\u{1F601}a"),
("a\u{1F600}", "a\u{1F601}"),
("l1\r\nl2\r\n", "l1\r\nl2\r\nl3\r\n"),
("l1\r\nl2\r\n", "l1\nl2\n"),
("a\r\nb", "a\r\n\r\nb"),
("a\r\n", "a\r\n\r\n"),
("", "x"),
("x", ""),
] {
let e = minimal_edit(a, b).unwrap();
assert_eq!(applied(a, &e), b, "{a:?} -> {b:?}: {e:?}");
}
let e = minimal_edit("\u{1F600}a", "\u{1F601}a").unwrap();
assert_eq!(
(e.range.start, e.range.end),
(Position::new(0, 0), Position::new(0, 2))
);
let e = minimal_edit("l1\r\nl2\r\n", "l1\r\nl2\r\nl3\r\n").unwrap();
assert_eq!(e.range.start, Position::new(2, 0));
let e = minimal_edit("a\rb", "a\r\nb").unwrap();
assert_eq!(
(e.range.start, e.new_text.as_str()),
(Position::new(1, 0), "\n")
);
}
fn notify(method: &str, params: serde_json::Value) -> Message {
Message::Notification(Notification {
method: method.to_string(),
params,
})
}
#[test]
fn saves_queued_together_are_checked_once() {
let dir = super::core_common::tempdir("htl-cli-lsp", "superseded");
std::fs::write(dir.join("htl.toml"), "[layout]\nsource = \".\"\n").unwrap();
std::fs::write(dir.join("a.tl"), "local n: number = \"x\"\nreturn n\n").unwrap();
let uri = uri_of(&dir.join("a.tl")).unwrap();
let doc = serde_json::json!({ "textDocument": { "uri": uri.as_str() } });
let (server_side, client) = Connection::memory();
let send = |m: Message| client.sender.send(m).unwrap();
send(Message::Request(Request::new(
1.into(),
"initialize".to_string(),
serde_json::json!({ "capabilities": {} }),
)));
send(notify("initialized", serde_json::json!({})));
send(notify(
"textDocument/didOpen",
serde_json::json!({ "textDocument": {
"uri": uri.as_str(), "languageId": "teal", "version": 1, "text": ""
} }),
));
for _ in 0..3 {
send(notify("textDocument/didSave", doc.clone()));
}
let server = std::thread::spawn(move || run_on(&server_side).unwrap());
let is_publish = |m: &Message| matches!(m, Message::Notification(n) if n.method == PublishDiagnostics::METHOD);
loop {
let m = client
.receiver
.recv_timeout(Duration::from_secs(60))
.unwrap();
if is_publish(&m) {
break;
}
}
send(Message::Request(Request::new(
2.into(),
"shutdown".to_string(),
serde_json::Value::Null,
)));
send(notify("exit", serde_json::Value::Null));
let code = server.join().unwrap();
assert_eq!(format!("{code:?}"), format!("{:?}", ExitCode::SUCCESS));
let rest: Vec<Message> = client.receiver.try_iter().collect();
assert!(!rest.iter().any(is_publish), "{rest:?}");
}
}