john-slop/src/lsp.rs

632 lines
22 KiB
Rust

//! Language Server Protocol state and adapter implementation.
use std::{collections::HashMap, ops::Range as ByteRange};
use tokio::sync::RwLock;
use tower_lsp_server::{
Client, LanguageServer,
jsonrpc::{Error as JsonError, Result as JsonResult},
ls_types::{
CodeAction, CodeActionKind, CodeActionOptions, CodeActionOrCommand, CodeActionParams,
CodeActionProviderCapability, CodeActionResponse, Diagnostic, DiagnosticSeverity,
DidChangeConfigurationParams, DidChangeTextDocumentParams, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, DocumentChanges, InitializeParams, InitializeResult,
InitializedParams, MessageType, NumberOrString, OneOf,
OptionalVersionedTextDocumentIdentifier, Position, Range, ServerCapabilities, ServerInfo,
TextDocumentEdit, TextDocumentSyncCapability, TextDocumentSyncKind,
TextEdit as LspTextEdit, Uri, WorkspaceEdit,
},
};
use crate::{Config, ConfigLoader, plan_replacements};
/// Failure to convert a canonical UTF-8 byte offset into an LSP position.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PositionError {
/// The requested offset lies beyond the indexed snapshot.
#[error("byte offset {offset} is outside a {source_len}-byte document")]
OutOfBounds {
/// Requested UTF-8 byte offset.
offset: usize,
/// Indexed snapshot length in bytes.
source_len: usize,
},
/// The requested offset splits a UTF-8 scalar value.
#[error("byte offset {offset} is not a UTF-8 character boundary")]
InvalidCharacterBoundary {
/// Invalid UTF-8 byte offset.
offset: usize,
},
/// The document is too large for the protocol's unsigned coordinates.
#[error("document position exceeds the LSP coordinate limit")]
CoordinateOverflow,
}
/// Indexed conversion from UTF-8 byte offsets to zero-based LSP UTF-16 positions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LineIndex<'source> {
source: &'source str,
line_starts: Vec<usize>,
}
impl<'source> LineIndex<'source> {
/// Index every logical line in one immutable source snapshot.
pub fn new(source: &'source str) -> Self {
let mut line_starts = vec![0];
line_starts.extend(
source
.match_indices('\n')
.map(|(offset, _)| offset.saturating_add(1)),
);
Self {
source,
line_starts,
}
}
/// Convert a canonical byte offset, including EOF, into an LSP position.
pub fn position(&self, offset: usize) -> Result<Position, PositionError> {
if offset > self.source.len() {
return Err(PositionError::OutOfBounds {
offset,
source_len: self.source.len(),
});
}
if !self.source.is_char_boundary(offset) {
return Err(PositionError::InvalidCharacterBoundary { offset });
}
let line_index = self.line_starts.partition_point(|start| *start <= offset) - 1;
let line_start = self.line_starts[line_index];
let line_end = self
.line_starts
.get(line_index + 1)
.map_or(self.source.len(), |next_start| next_start - 1);
let content_end = line_end
.checked_sub(1)
.filter(|candidate| self.source.as_bytes().get(*candidate) == Some(&b'\r'))
.unwrap_or(line_end);
let effective_offset = offset.min(content_end);
let utf16_units = self.source[line_start..effective_offset]
.encode_utf16()
.count();
Ok(Position {
line: u32::try_from(line_index).map_err(|_| PositionError::CoordinateOverflow)?,
character: u32::try_from(utf16_units).map_err(|_| PositionError::CoordinateOverflow)?,
})
}
/// Convert a UTF-8 byte range into an LSP UTF-16 range.
pub fn range(&self, bytes: ByteRange<usize>) -> Result<Range, PositionError> {
Ok(Range {
start: self.position(bytes.start)?,
end: self.position(bytes.end)?,
})
}
}
/// Convert configured replacement edits into stable LSP diagnostics.
pub fn replacement_diagnostics(
config: &Config,
source: &str,
version: i32,
) -> Result<Vec<Diagnostic>, PositionError> {
let index = LineIndex::new(source);
plan_replacements(config, source)
.into_iter()
.map(|edit| {
let matched = &source[edit.byte_span.clone()];
let rule = config
.replacements
.iter()
.find(|rule| rule.enabled && rule.source == matched);
let code = rule.map(|rule| NumberOrString::String(rule.id.clone()));
let rationale = rule.map_or("configured non-ASCII text", |rule| &rule.rationale);
let mut diagnostic = Diagnostic::new(
index.range(edit.byte_span)?,
Some(DiagnosticSeverity::WARNING),
code,
Some("john-slop".to_owned()),
format!(
"Replace {matched:?} with ASCII {:?}: {rationale}",
edit.replacement
),
None,
None,
);
diagnostic.data = Some(serde_json::json!({ "version": version }));
Ok(diagnostic)
})
.collect()
}
fn replacement_code_actions(
config: &Config,
uri: &Uri,
snapshot: &OpenDocument,
requested_range: &Range,
diagnostics: &[Diagnostic],
) -> Result<Vec<CodeActionOrCommand>, PositionError> {
let index = LineIndex::new(&snapshot.text);
plan_replacements(config, &snapshot.text)
.into_iter()
.filter_map(|edit| {
let range = match index.range(edit.byte_span) {
Ok(range) => range,
Err(error) => return Some(Err(error)),
};
if !ranges_overlap(&range, requested_range) {
return None;
}
let related = diagnostics
.iter()
.find(|diagnostic| {
diagnostic.source.as_deref() == Some("john-slop") && diagnostic.range == range
})
.cloned();
let text_edit = LspTextEdit::new(range, edit.replacement.clone());
let workspace_edit = WorkspaceEdit {
changes: None,
document_changes: Some(DocumentChanges::Edits(vec![TextDocumentEdit {
text_document: OptionalVersionedTextDocumentIdentifier::new(
uri.clone(),
snapshot.version,
),
edits: vec![OneOf::Left(text_edit)],
}])),
change_annotations: None,
};
Some(Ok(CodeActionOrCommand::CodeAction(CodeAction {
title: format!("Replace with ASCII {:?}", edit.replacement),
kind: Some(CodeActionKind::QUICKFIX),
diagnostics: related.map(|diagnostic| vec![diagnostic]),
edit: Some(workspace_edit),
is_preferred: Some(true),
data: Some(serde_json::json!({ "version": snapshot.version })),
..CodeAction::default()
})))
})
.collect()
}
fn ranges_overlap(left: &Range, right: &Range) -> bool {
left.start <= right.end && right.start <= left.end
}
fn requested_version_is_current(diagnostics: &[Diagnostic], version: i32) -> bool {
diagnostics
.iter()
.filter(|diagnostic| diagnostic.source.as_deref() == Some("john-slop"))
.filter_map(|diagnostic| diagnostic.data.as_ref())
.filter_map(|data| data.get("version"))
.filter_map(serde_json::Value::as_i64)
.all(|requested| requested == i64::from(version))
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct OpenDocument {
version: i32,
language_id: String,
text: String,
}
#[derive(Debug, Default)]
struct DocumentStore {
documents: HashMap<Uri, OpenDocument>,
}
impl DocumentStore {
fn open(&mut self, params: DidOpenTextDocumentParams) {
let document = params.text_document;
self.documents.insert(
document.uri,
OpenDocument {
version: document.version,
language_id: document.language_id,
text: document.text,
},
);
}
fn change(&mut self, params: DidChangeTextDocumentParams) -> bool {
let Some(document) = self.documents.get_mut(&params.text_document.uri) else {
return false;
};
if params.text_document.version <= document.version {
return false;
}
let [change] = params.content_changes.as_slice() else {
return false;
};
if change.range.is_some() {
return false;
}
document.version = params.text_document.version;
document.text.clone_from(&change.text);
true
}
fn close(&mut self, uri: &Uri) -> bool {
self.documents.remove(uri).is_some()
}
fn get(&self, uri: &Uri) -> Option<OpenDocument> {
self.documents.get(uri).cloned()
}
fn uris(&self) -> Vec<Uri> {
let mut uris = self.documents.keys().cloned().collect::<Vec<_>>();
uris.sort();
uris
}
}
/// Stdio LSP backend backed by authoritative snapshots of open documents.
#[derive(Debug)]
pub struct LspBackend {
client: Client,
config_loader: ConfigLoader,
config: RwLock<Config>,
documents: RwLock<DocumentStore>,
log_json: bool,
}
impl LspBackend {
/// Construct a backend from the same resolved configuration used by other adapters.
pub fn new(client: Client, config_loader: ConfigLoader, config: Config) -> Self {
Self {
client,
config_loader,
config: RwLock::new(config),
documents: RwLock::new(DocumentStore::default()),
log_json: false,
}
}
/// Enable newline-delimited JSON lifecycle logging on standard error.
#[must_use]
pub const fn with_json_logging(mut self, enabled: bool) -> Self {
self.log_json = enabled;
self
}
fn log(&self, level: &str, event: &str, uri: Option<&Uri>, version: Option<i32>) {
if !self.log_json {
return;
}
let mut record = serde_json::json!({
"component": "john-slop-lsp",
"event": event,
"level": level,
});
if let Some(uri) = uri {
record["uri"] = serde_json::Value::String(uri.as_str().to_owned());
}
if let Some(version) = version {
record["version"] = serde_json::Value::from(version);
}
eprintln!("{record}");
}
async fn publish_replacements(&self, uri: Uri) {
let Some(snapshot) = self.documents.read().await.get(&uri) else {
return;
};
let config = self.config.read().await.clone();
let diagnostics =
replacement_diagnostics(&config, &snapshot.text, snapshot.version).unwrap_or_default();
let is_current = self
.documents
.read()
.await
.get(&uri)
.is_some_and(|document| document.version == snapshot.version);
if is_current {
self.client
.publish_diagnostics(uri, diagnostics, Some(snapshot.version))
.await;
}
}
}
impl LanguageServer for LspBackend {
async fn initialize(&self, _: InitializeParams) -> JsonResult<InitializeResult> {
Ok(InitializeResult {
server_info: Some(ServerInfo {
name: "john-slop-lsp".to_owned(),
version: Some(env!("CARGO_PKG_VERSION").to_owned()),
}),
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
code_action_provider: Some(CodeActionProviderCapability::Options(
CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),
resolve_provider: Some(false),
..CodeActionOptions::default()
},
)),
..ServerCapabilities::default()
},
offset_encoding: None,
})
}
async fn initialized(&self, _: InitializedParams) {
self.log("info", "initialized", None, None);
}
async fn shutdown(&self) -> JsonResult<()> {
self.log("info", "shutdown", None, None);
Ok(())
}
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri.clone();
let version = params.text_document.version;
self.documents.write().await.open(params);
self.log("info", "document-opened", Some(&uri), Some(version));
self.publish_replacements(uri).await;
}
async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.clone();
let version = params.text_document.version;
if self.documents.write().await.change(params) {
self.log("info", "document-changed", Some(&uri), Some(version));
self.publish_replacements(uri).await;
}
}
async fn did_close(&self, params: DidCloseTextDocumentParams) {
let uri = params.text_document.uri;
self.documents.write().await.close(&uri);
self.log("info", "document-closed", Some(&uri), None);
self.client.publish_diagnostics(uri, Vec::new(), None).await;
}
async fn did_change_configuration(&self, _: DidChangeConfigurationParams) {
let config = match self.config_loader.load() {
Ok(config) => config,
Err(error) => {
self.log("error", "configuration-reload-failed", None, None);
self.client
.show_message(
MessageType::ERROR,
format!("John Slop configuration reload failed: {error}"),
)
.await;
return;
}
};
*self.config.write().await = config;
self.log("info", "configuration-reloaded", None, None);
let uris = self.documents.read().await.uris();
for uri in uris {
self.publish_replacements(uri).await;
}
}
async fn code_action(
&self,
params: CodeActionParams,
) -> JsonResult<Option<CodeActionResponse>> {
let uri = params.text_document.uri;
let Some(snapshot) = self.documents.read().await.get(&uri) else {
return Ok(None);
};
if !requested_version_is_current(&params.context.diagnostics, snapshot.version) {
return Err(JsonError::invalid_params(
"code action diagnostics refer to a stale document version",
));
}
let config = self.config.read().await.clone();
let actions = replacement_code_actions(
&config,
&uri,
&snapshot,
&params.range,
&params.context.diagnostics,
)
.map_err(|error| JsonError::invalid_params(error.to_string()))?;
Ok(Some(actions))
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use tower_lsp_server::ls_types::{
DidChangeTextDocumentParams, DidOpenTextDocumentParams, NumberOrString, Position,
TextDocumentContentChangeEvent, TextDocumentItem, Uri, VersionedTextDocumentIdentifier,
};
use super::{
DocumentStore, LineIndex, OpenDocument, PositionError, replacement_code_actions,
replacement_diagnostics, requested_version_is_current,
};
fn uri() -> Uri {
Uri::from_str("file:///tmp/example.txt").unwrap()
}
#[test]
fn tracks_language_version_and_full_document_text() {
let mut store = DocumentStore::default();
store.open(DidOpenTextDocumentParams {
text_document: TextDocumentItem::new(
uri(),
"plaintext".to_owned(),
4,
"old".to_owned(),
),
});
assert!(!store.change(DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier::new(uri(), 3),
content_changes: vec![TextDocumentContentChangeEvent {
range: None,
range_length: None,
text: "stale".to_owned(),
}],
}));
assert!(store.change(DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier::new(uri(), 5),
content_changes: vec![TextDocumentContentChangeEvent {
range: None,
range_length: None,
text: "current".to_owned(),
}],
}));
let document = &store.documents[&uri()];
assert_eq!(document.language_id, "plaintext");
assert_eq!(document.version, 5);
assert_eq!(document.text, "current");
}
#[test]
fn ignores_unknown_incremental_and_ambiguous_changes() {
let mut store = DocumentStore::default();
let change = TextDocumentContentChangeEvent {
range: None,
range_length: None,
text: "replacement".to_owned(),
};
assert!(!store.change(DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier::new(uri(), 1),
content_changes: vec![change.clone()],
}));
store.open(DidOpenTextDocumentParams {
text_document: TextDocumentItem::new(
uri(),
"text".to_owned(),
1,
"original".to_owned(),
),
});
assert!(!store.change(DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier::new(uri(), 2),
content_changes: vec![change.clone(), change],
}));
assert_eq!(store.documents[&uri()].text, "original");
assert!(store.close(&uri()));
assert!(!store.close(&uri()));
}
#[test]
fn maps_ascii_crlf_and_every_eof_shape() {
let index = LineIndex::new("ab\r\ncd\n");
assert_eq!(index.position(0).unwrap(), Position::new(0, 0));
assert_eq!(index.position(2).unwrap(), Position::new(0, 2));
assert_eq!(index.position(3).unwrap(), Position::new(0, 2));
assert_eq!(index.position(4).unwrap(), Position::new(1, 0));
assert_eq!(index.position(6).unwrap(), Position::new(1, 2));
assert_eq!(index.position(7).unwrap(), Position::new(2, 0));
assert_eq!(LineIndex::new("").position(0).unwrap(), Position::new(0, 0));
assert_eq!(
LineIndex::new("last").position(4).unwrap(),
Position::new(0, 4)
);
}
#[test]
fn maps_emoji_astral_and_combining_text_to_utf16_units() {
let source = "a😀e\u{301}\n𐐷z";
let index = LineIndex::new(source);
assert_eq!(index.position(1).unwrap(), Position::new(0, 1));
assert_eq!(index.position(5).unwrap(), Position::new(0, 3));
assert_eq!(index.position(6).unwrap(), Position::new(0, 4));
assert_eq!(index.position(8).unwrap(), Position::new(0, 5));
assert_eq!(index.position(9).unwrap(), Position::new(1, 0));
assert_eq!(index.position(13).unwrap(), Position::new(1, 2));
assert_eq!(index.position(source.len()).unwrap(), Position::new(1, 3));
assert_eq!(
index.range(1..8).unwrap(),
tower_lsp_server::ls_types::Range::new(Position::new(0, 1), Position::new(0, 5))
);
}
#[test]
fn rejects_offsets_outside_utf8_boundaries() {
let index = LineIndex::new("😀");
assert_eq!(
index.position(1),
Err(PositionError::InvalidCharacterBoundary { offset: 1 })
);
assert_eq!(
index.position(5),
Err(PositionError::OutOfBounds {
offset: 5,
source_len: 4,
})
);
}
#[test]
fn replacement_diagnostics_have_utf16_ranges_stable_codes_and_source() {
let diagnostics =
replacement_diagnostics(&crate::Config::embedded(), "😀— ordinary prose", 7).unwrap();
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].range.start, Position::new(0, 2));
assert_eq!(diagnostics[0].range.end, Position::new(0, 3));
assert_eq!(
diagnostics[0].code,
Some(NumberOrString::String("em-dash".to_owned()))
);
assert_eq!(diagnostics[0].source.as_deref(), Some("john-slop"));
assert!(diagnostics[0].message.contains("ASCII \"-\""));
assert_eq!(
diagnostics[0].data,
Some(serde_json::json!({ "version": 7 }))
);
}
#[test]
fn quick_fix_uses_exact_versioned_replacement_edit() {
let config = crate::Config::embedded();
let source = "😀—text";
let document = OpenDocument {
version: 9,
language_id: "plaintext".to_owned(),
text: source.to_owned(),
};
let diagnostics = replacement_diagnostics(&config, source, 9).unwrap();
let actions = replacement_code_actions(
&config,
&uri(),
&document,
&diagnostics[0].range,
&diagnostics,
)
.unwrap();
let value = serde_json::to_value(&actions).unwrap();
assert_eq!(actions.len(), 1);
assert_eq!(value[0]["kind"], "quickfix");
assert_eq!(
value[0]["edit"]["documentChanges"][0]["textDocument"]["version"],
9
);
assert_eq!(
value[0]["edit"]["documentChanges"][0]["edits"][0]["newText"],
"-"
);
assert_eq!(
value[0]["edit"]["documentChanges"][0]["edits"][0]["range"]["start"]["character"],
2
);
assert!(requested_version_is_current(&diagnostics, 9));
assert!(!requested_version_is_current(&diagnostics, 10));
}
}