Skip to content

Commit

Permalink
feat: move biome_lsp/converters to a separate crate (#4407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh authored Oct 28, 2024
1 parent 64677a9 commit eb13885
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 57 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ biome_json_factory = { version = "0.5.7", path = "./crates/biome_json_
biome_json_formatter = { version = "0.5.7", path = "./crates/biome_json_formatter" }
biome_json_parser = { version = "0.5.7", path = "./crates/biome_json_parser" }
biome_json_syntax = { version = "0.5.7", path = "./crates/biome_json_syntax" }
biome_lsp_converters = { version = "0.1.0", path = "./crates/biome_lsp_converters" }
biome_markdown_factory = { version = "0.0.1", path = "./crates/biome_markdown_factory" }
biome_markdown_parser = { version = "0.0.1", path = "./crates/biome_markdown_parser" }
biome_markdown_syntax = { version = "0.0.1", path = "./crates/biome_markdown_syntax" }
Expand Down
35 changes: 18 additions & 17 deletions crates/biome_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ version = "0.0.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { workspace = true }
biome_analyze = { workspace = true }
biome_configuration = { workspace = true }
biome_console = { workspace = true }
biome_deserialize = { workspace = true }
biome_diagnostics = { workspace = true }
biome_fs = { workspace = true }
biome_rowan = { workspace = true }
biome_service = { workspace = true }
biome_text_edit = { workspace = true }
futures = "0.3.30"
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["rt", "io-std"] }
tower-lsp = { version = "0.20.0" }
tracing = { workspace = true, features = ["attributes"] }
anyhow = { workspace = true }
biome_analyze = { workspace = true }
biome_configuration = { workspace = true }
biome_console = { workspace = true }
biome_deserialize = { workspace = true }
biome_diagnostics = { workspace = true }
biome_fs = { workspace = true }
biome_lsp_converters = { workspace = true }
biome_rowan = { workspace = true }
biome_service = { workspace = true }
biome_text_edit = { workspace = true }
futures = "0.3.30"
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["rt", "io-std"] }
tower-lsp = { version = "0.20.0" }
tracing = { workspace = true, features = ["attributes"] }

[dev-dependencies]
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_lsp/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::converters::{negotiated_encoding, PositionEncoding, WideEncoding};
use biome_analyze::SUPPRESSION_ACTION_CATEGORY;
use biome_lsp_converters::{negotiated_encoding, PositionEncoding, WideEncoding};
use tower_lsp::lsp_types::{
ClientCapabilities, CodeActionKind, CodeActionOptions, CodeActionProviderCapability,
DocumentOnTypeFormattingOptions, OneOf, PositionEncodingKind, ServerCapabilities,
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_lsp/src/documents.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::converters::line_index::LineIndex;
use biome_lsp_converters::line_index::LineIndex;

/// Represents an open [`textDocument`]. Can be cheaply cloned.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::converters::from_proto;
use crate::converters::line_index::LineIndex;
use crate::diagnostics::LspError;
use crate::session::Session;
use crate::utils;
use anyhow::{Context, Result};
use biome_analyze::{ActionCategory, RuleCategoriesBuilder, SourceActionKind};
use biome_diagnostics::Applicability;
use biome_fs::BiomePath;
use biome_lsp_converters::from_proto;
use biome_lsp_converters::line_index::LineIndex;
use biome_rowan::{TextRange, TextSize};
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use biome_service::workspace::{
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_lsp/src/handlers/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::converters::from_proto;
use crate::diagnostics::LspError;
use crate::session::Session;
use crate::utils::text_edit;
use anyhow::Context;
use biome_fs::BiomePath;
use biome_lsp_converters::from_proto;
use biome_rowan::{TextRange, TextSize};
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use biome_service::workspace::{
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_lsp/src/handlers/rename.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;

use crate::converters::from_proto;
use crate::diagnostics::LspError;
use crate::{session::Session, utils};
use anyhow::{Context, Result};
use biome_lsp_converters::from_proto;
use tower_lsp::lsp_types::{RenameParams, WorkspaceEdit};
use tracing::trace;

Expand Down
1 change: 0 additions & 1 deletion crates/biome_lsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod capabilities;
mod converters;
mod diagnostics;
mod documents;
mod extension_settings;
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_lsp/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::converters::{negotiated_encoding, PositionEncoding, WideEncoding};
use crate::diagnostics::LspError;
use crate::documents::Document;
use crate::extension_settings::ExtensionSettings;
Expand All @@ -11,6 +10,7 @@ use biome_console::markup;
use biome_deserialize::Merge;
use biome_diagnostics::{DiagnosticExt, Error, PrintDescription};
use biome_fs::{BiomePath, FileSystem};
use biome_lsp_converters::{negotiated_encoding, PositionEncoding, WideEncoding};
use biome_service::configuration::{
load_configuration, load_editorconfig, LoadedConfiguration, PartialConfigurationExt,
};
Expand Down
8 changes: 4 additions & 4 deletions crates/biome_lsp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::converters::line_index::LineIndex;
use crate::converters::{from_proto, to_proto, PositionEncoding};
use anyhow::{ensure, Context, Result};
use biome_analyze::ActionCategory;
use biome_console::fmt::Termcolor;
Expand All @@ -9,6 +7,8 @@ use biome_diagnostics::termcolor::NoColor;
use biome_diagnostics::{
Applicability, {Diagnostic, DiagnosticTags, Location, PrintDescription, Severity, Visit},
};
use biome_lsp_converters::line_index::LineIndex;
use biome_lsp_converters::{from_proto, to_proto, PositionEncoding};
use biome_rowan::{TextRange, TextSize};
use biome_service::workspace::CodeAction;
use biome_text_edit::{CompressedOp, DiffOp, TextEdit};
Expand Down Expand Up @@ -380,8 +380,8 @@ pub(crate) fn apply_document_changes(
#[cfg(test)]
mod tests {
use super::apply_document_changes;
use crate::converters::line_index::LineIndex;
use crate::converters::{PositionEncoding, WideEncoding};
use biome_lsp_converters::line_index::LineIndex;
use biome_lsp_converters::{PositionEncoding, WideEncoding};
use biome_text_edit::TextEdit;
use tower_lsp::lsp_types as lsp;
use tower_lsp::lsp_types::{Position, Range, TextDocumentContentChangeEvent};
Expand Down
25 changes: 25 additions & 0 deletions crates/biome_lsp_converters/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
authors.workspace = true
categories.workspace = true
description = "Biome's tools to convert between LSP and Biome's data structures"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "biome_lsp_converters"
repository.workspace = true
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { workspace = true }
biome_rowan = { workspace = true }
rustc-hash = { workspace = true }
tower-lsp = { version = "0.20.0" }

[dev-dependencies]
tower = { version = "0.4.13", features = ["timeout"] }

[lints]
workspace = true
3 changes: 3 additions & 0 deletions crates/biome_lsp_converters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `biome_lsp_converters`

The crate contains a set of converters to translate between `lsp-types` and `biome_rowan` (and vice versa) types.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::converters::line_index::LineIndex;
use crate::converters::{LineCol, PositionEncoding, WideLineCol};
use crate::line_index::LineIndex;
use crate::{LineCol, PositionEncoding, WideLineCol};
use anyhow::{Context, Result};
use biome_rowan::{TextRange, TextSize};
use tower_lsp::lsp_types;

/// The function is used to convert a LSP position to TextSize.
pub(crate) fn offset(
pub fn offset(
line_index: &LineIndex,
position: lsp_types::Position,
position_encoding: PositionEncoding,
Expand All @@ -30,7 +30,7 @@ pub(crate) fn offset(
}

/// The function is used to convert a LSP range to TextRange.
pub(crate) fn text_range(
pub fn text_range(
line_index: &LineIndex,
range: lsp_types::Range,
position_encoding: PositionEncoding,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use biome_rowan::TextSize;
use tower_lsp::lsp_types::{ClientCapabilities, PositionEncodingKind};

pub(crate) mod from_proto;
pub(crate) mod line_index;
pub(crate) mod to_proto;
pub mod from_proto;
pub mod line_index;
pub mod to_proto;

pub(crate) fn negotiated_encoding(capabilities: &ClientCapabilities) -> PositionEncoding {
pub fn negotiated_encoding(capabilities: &ClientCapabilities) -> PositionEncoding {
let client_encodings = match &capabilities.general {
Some(general) => general.position_encodings.as_deref().unwrap_or_default(),
None => &[],
Expand Down Expand Up @@ -36,11 +36,11 @@ pub enum WideEncoding {
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct LineCol {
pub struct LineCol {
/// Zero-based
pub(crate) line: u32,
pub line: u32,
/// Zero-based utf8 offset
pub(crate) col: u32,
pub col: u32,
}

/// Deliberately not a generic type and different from `LineCol`.
Expand All @@ -53,11 +53,11 @@ pub struct WideLineCol {
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) struct WideChar {
pub struct WideChar {
/// Start offset of a character inside a line, zero-based
pub(crate) start: TextSize,
pub start: TextSize,
/// End offset of a character inside a line, zero-based
pub(crate) end: TextSize,
pub end: TextSize,
}

impl WideChar {
Expand All @@ -84,11 +84,11 @@ impl WideChar {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
use std::mem;

use crate::converters::{LineCol, WideChar, WideEncoding, WideLineCol};
use biome_rowan::TextSize;
use rustc_hash::FxHashMap;

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct LineIndex {
pub struct LineIndex {
/// Offset the beginning of each line, zero-based.
pub(crate) newlines: Vec<TextSize>,
pub newlines: Vec<TextSize>,
/// List of non-ASCII characters on each line.
pub(crate) line_wide_chars: FxHashMap<u32, Vec<WideChar>>,
pub line_wide_chars: FxHashMap<u32, Vec<WideChar>>,
}

impl LineIndex {
Expand Down Expand Up @@ -67,10 +68,15 @@ impl LineIndex {
}

/// Return the number of lines in the index, clamped to [u32::MAX]
pub(crate) fn len(&self) -> u32 {
pub fn len(&self) -> u32 {
self.newlines.len().try_into().unwrap_or(u32::MAX)
}

/// Return `true` if the index contains no lines.
pub fn is_empty(&self) -> bool {
self.newlines.is_empty()
}

pub fn line_col(&self, offset: TextSize) -> Option<LineCol> {
let line = self.newlines.partition_point(|&it| it <= offset) - 1;
let line_start_offset = self.newlines.get(line)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::converters::line_index::LineIndex;
use crate::converters::PositionEncoding;
use crate::line_index::LineIndex;
use crate::PositionEncoding;
use anyhow::{Context, Result};
use biome_rowan::{TextRange, TextSize};
use tower_lsp::lsp_types;

/// The function is used to convert TextSize to a LSP position.
pub(crate) fn position(
pub fn position(
line_index: &LineIndex,
offset: TextSize,
position_encoding: PositionEncoding,
Expand All @@ -28,7 +28,7 @@ pub(crate) fn position(
}

/// The function is used to convert TextRange to a LSP range.
pub(crate) fn range(
pub fn range(
line_index: &LineIndex,
range: TextRange,
position_encoding: PositionEncoding,
Expand Down
3 changes: 3 additions & 0 deletions knope.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ versioned_files = ["crates/biome_json_parser/Cargo.toml"]
[packages.biome_json_syntax]
changelog = "crates/biome_json_syntax/CHANGELOG.md"
versioned_files = ["crates/biome_json_syntax/Cargo.toml"]
[packages.biome_lsp_converters]
changelog = "crates/biome_lsp_converters/CHANGELOG.md"
versioned_files = ["crates/biome_lsp_converters/Cargo.toml"]
[packages.biome_markup]
changelog = "crates/biome_markup/CHANGELOG.md"
versioned_files = ["crates/biome_markup/Cargo.toml"]
Expand Down

0 comments on commit eb13885

Please sign in to comment.