Skip to content

refactor: move lsp converters to lsp crate #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/pgt_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pgt_configuration = { workspace = true }
pgt_console = { workspace = true }
pgt_diagnostics = { workspace = true }
pgt_fs = { workspace = true }
pgt_lsp_converters = { workspace = true }
pgt_text_edit = { workspace = true }
pgt_text_size.workspace = true
pgt_workspace = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::line_index::LineIndex;
use crate::{LineCol, PositionEncoding, WideLineCol};
use crate::adapters::line_index::LineIndex;
use crate::adapters::{LineCol, PositionEncoding, WideLineCol};
use anyhow::{Context, Result};
use pgt_text_size::{TextRange, TextSize};
use tower_lsp::lsp_types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::mem;
use pgt_text_size::TextSize;
use rustc_hash::FxHashMap;

use crate::{LineCol, WideChar, WideEncoding, WideLineCol};
use crate::adapters::{LineCol, WideChar, WideEncoding, WideLineCol};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LineIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use pgt_text_size::TextSize;
use tower_lsp::lsp_types::{ClientCapabilities, PositionEncodingKind};

pub mod from_proto;
pub mod from_lsp;
pub mod line_index;
pub mod to_proto;
pub mod to_lsp;

pub fn negotiated_encoding(capabilities: &ClientCapabilities) -> PositionEncoding {
let client_encodings = match &capabilities.general {
Expand Down Expand Up @@ -86,11 +86,11 @@ impl WideChar {

#[cfg(test)]
mod tests {
use crate::WideEncoding::{Utf16, Utf32};
use crate::from_proto::offset;
use crate::line_index::LineIndex;
use crate::to_proto::position;
use crate::{LineCol, PositionEncoding, WideEncoding};
use crate::adapters::WideEncoding::{Utf16, Utf32};
use crate::adapters::from_lsp::offset;
use crate::adapters::line_index::LineIndex;
use crate::adapters::to_lsp::position;
use crate::adapters::{LineCol, PositionEncoding, WideEncoding};
use pgt_text_size::TextSize;
use tower_lsp::lsp_types::Position;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::PositionEncoding;
use crate::line_index::LineIndex;
use crate::adapters::PositionEncoding;
use crate::adapters::line_index::LineIndex;
use anyhow::{Context, Result};
use pgt_text_size::{TextRange, TextSize};
use tower_lsp::lsp_types;
Expand Down
9 changes: 4 additions & 5 deletions crates/pgt_lsp/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use pgt_lsp_converters::{PositionEncoding, WideEncoding, negotiated_encoding};
use crate::adapters::{PositionEncoding, WideEncoding, negotiated_encoding};
use pgt_workspace::code_actions::{CommandActionCategory, CommandActionCategoryIter};
use strum::{EnumIter, IntoEnumIterator};
use tower_lsp::lsp_types::{
ClientCapabilities, CodeActionOptions, CompletionOptions, ExecuteCommandOptions,
PositionEncodingKind, SaveOptions, ServerCapabilities, TextDocumentSyncCapability,
TextDocumentSyncKind, TextDocumentSyncOptions, TextDocumentSyncSaveOptions,
WorkDoneProgressOptions,
ClientCapabilities, CompletionOptions, ExecuteCommandOptions, PositionEncodingKind,
SaveOptions, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind,
TextDocumentSyncOptions, TextDocumentSyncSaveOptions, WorkDoneProgressOptions,
};

use crate::handlers::code_actions::command_id;
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_lsp/src/documents.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pgt_lsp_converters::line_index::LineIndex;
use crate::adapters::line_index::LineIndex;

/// Represents an open [`textDocument`]. Can be cheaply cloned.
///
Expand Down
9 changes: 6 additions & 3 deletions crates/pgt_lsp/src/handlers/helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::session::Session;
use crate::{
adapters::{self, from_lsp},
session::Session,
};
use pgt_text_size::TextSize;
use tower_lsp::lsp_types;

Expand All @@ -16,10 +19,10 @@ pub fn get_cursor_position(
.map(|doc| doc.line_index)
.map_err(|_| anyhow::anyhow!("Document not found."))?;

let cursor_pos = pgt_lsp_converters::from_proto::offset(
let cursor_pos = from_lsp::offset(
&line_index,
position,
pgt_lsp_converters::negotiated_encoding(client_capabilities),
adapters::negotiated_encoding(client_capabilities),
)?;

Ok(cursor_pos)
Expand Down
4 changes: 2 additions & 2 deletions crates/pgt_lsp/src/handlers/text_document.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::adapters::from_lsp;
use crate::{
diagnostics::LspError, documents::Document, session::Session, utils::apply_document_changes,
};
use anyhow::Result;
use pgt_lsp_converters::from_proto::text_range;
use pgt_workspace::workspace::{
ChangeFileParams, ChangeParams, CloseFileParams, GetFileContentParams, OpenFileParams,
};
Expand Down Expand Up @@ -73,7 +73,7 @@ pub(crate) async fn did_change(
.iter()
.map(|c| ChangeParams {
range: c.range.and_then(|r| {
text_range(&old_doc.line_index, r, session.position_encoding()).ok()
from_lsp::text_range(&old_doc.line_index, r, session.position_encoding()).ok()
}),
text: c.text.clone(),
})
Expand Down
1 change: 1 addition & 0 deletions crates/pgt_lsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod adapters;
mod capabilities;
mod diagnostics;
mod documents;
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_lsp/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::adapters::{PositionEncoding, WideEncoding, negotiated_encoding};
use crate::diagnostics::LspError;
use crate::documents::Document;
use crate::utils;
Expand All @@ -8,7 +9,6 @@ use pgt_analyse::RuleCategoriesBuilder;
use pgt_configuration::ConfigurationPathHint;
use pgt_diagnostics::{DiagnosticExt, Error};
use pgt_fs::{FileSystem, PgTPath};
use pgt_lsp_converters::{PositionEncoding, WideEncoding, negotiated_encoding};
use pgt_workspace::Workspace;
use pgt_workspace::configuration::{LoadedConfiguration, load_configuration};
use pgt_workspace::settings::PartialConfigurationExt;
Expand Down
21 changes: 10 additions & 11 deletions crates/pgt_lsp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::adapters::line_index::LineIndex;
use crate::adapters::{PositionEncoding, from_lsp, to_lsp};
use anyhow::{Context, Result, ensure};
use pgt_console::MarkupBuf;
use pgt_console::fmt::Termcolor;
use pgt_console::fmt::{self, Formatter};
use pgt_diagnostics::termcolor::NoColor;
use pgt_diagnostics::{Diagnostic, DiagnosticTags, Location, PrintDescription, Severity, Visit};
use pgt_lsp_converters::line_index::LineIndex;
use pgt_lsp_converters::{PositionEncoding, from_proto, to_proto};
use pgt_text_edit::{CompressedOp, DiffOp, TextEdit};
use pgt_text_size::{TextRange, TextSize};
use std::any::Any;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub(crate) fn text_edit(
offset += range.len();
}
CompressedOp::DiffOp(DiffOp::Insert { range }) => {
let start = to_proto::position(line_index, offset, position_encoding)?;
let start = to_lsp::position(line_index, offset, position_encoding)?;

// Merge with a previous delete operation if possible
let last_edit = result.last_mut().filter(|text_edit| {
Expand All @@ -54,9 +54,9 @@ pub(crate) fn text_edit(
}
}
CompressedOp::DiffOp(DiffOp::Delete { range }) => {
let start = to_proto::position(line_index, offset, position_encoding)?;
let start = to_lsp::position(line_index, offset, position_encoding)?;
offset += range.len();
let end = to_proto::position(line_index, offset, position_encoding)?;
let end = to_lsp::position(line_index, offset, position_encoding)?;

result.push(lsp::TextEdit {
range: lsp::Range::new(start, end),
Expand Down Expand Up @@ -108,7 +108,7 @@ pub(crate) fn diagnostic_to_lsp<D: Diagnostic>(
} else {
span
};
let span = to_proto::range(line_index, span, position_encoding)
let span = to_lsp::range(line_index, span, position_encoding)
.context("failed to convert diagnostic span to LSP range")?;

let severity = match diagnostic.severity() {
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Visit for RelatedInformationVisitor<'_> {
None => return Ok(()),
};

let range = match to_proto::range(self.line_index, span, self.position_encoding) {
let range = match to_lsp::range(self.line_index, span, self.position_encoding) {
Ok(range) => range,
Err(_) => return Ok(()),
};
Expand Down Expand Up @@ -293,7 +293,7 @@ pub(crate) fn apply_document_changes(
line_index = LineIndex::new(&text);
}
index_valid = range.start.line;
if let Ok(range) = from_proto::text_range(&line_index, range, position_encoding) {
if let Ok(range) = from_lsp::text_range(&line_index, range, position_encoding) {
text.replace_range(Range::<usize>::from(range), &change.text);
}
}
Expand All @@ -304,9 +304,8 @@ pub(crate) fn apply_document_changes(

#[cfg(test)]
mod tests {

use pgt_lsp_converters::PositionEncoding;
use pgt_lsp_converters::line_index::LineIndex;
use crate::adapters::PositionEncoding;
use crate::adapters::line_index::LineIndex;
use pgt_text_edit::TextEdit;
use tower_lsp::lsp_types as lsp;

Expand Down
23 changes: 0 additions & 23 deletions crates/pgt_lsp_converters/Cargo.toml

This file was deleted.