diff --git a/Cargo.lock b/Cargo.lock index 6392ce49..28e0a3d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1794,13 +1794,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "line_index" -version = "0.0.0" -dependencies = [ - "text-size", -] - [[package]] name = "linked-hash-map" version = "0.5.6" @@ -2222,10 +2215,10 @@ dependencies = [ "pglt_diagnostics", "pglt_query_ext", "pglt_schema_cache", + "pglt_text_size", "rustc-hash 2.1.0", "schemars", "serde", - "text-size", ] [[package]] @@ -2284,8 +2277,8 @@ version = "0.0.0" dependencies = [ "anyhow", "async-std", + "pglt_text_size", "sqlx", - "text-size", ] [[package]] @@ -2295,11 +2288,11 @@ dependencies = [ "async-std", "pglt_schema_cache", "pglt_test_utils", + "pglt_text_size", "pglt_treesitter_queries", "serde", "serde_json", "sqlx", - "text-size", "tokio", "tree-sitter", "tree_sitter_sql", @@ -2317,11 +2310,11 @@ dependencies = [ "pglt_analyser", "pglt_console", "pglt_diagnostics", + "pglt_text_size", "rustc-hash 2.1.0", "schemars", "serde", "serde_json", - "text-size", "toml", ] @@ -2330,10 +2323,10 @@ name = "pglt_console" version = "0.0.0" dependencies = [ "pglt_markup", + "pglt_text_size", "schemars", "serde", "termcolor", - "text-size", "trybuild", "unicode-segmentation", "unicode-width", @@ -2350,11 +2343,11 @@ dependencies = [ "pglt_diagnostics_categories", "pglt_diagnostics_macros", "pglt_text_edit", + "pglt_text_size", "schemars", "serde", "serde_json", "termcolor", - "text-size", "unicode-width", ] @@ -2409,8 +2402,8 @@ dependencies = [ "pg_query", "pglt_diagnostics", "pglt_lexer_codegen", + "pglt_text_size", "regex", - "text-size", ] [[package]] @@ -2438,12 +2431,12 @@ dependencies = [ "pglt_lsp_converters", "pglt_test_utils", "pglt_text_edit", + "pglt_text_size", "pglt_workspace", "rustc-hash 2.1.0", "serde", "serde_json", "sqlx", - "text-size", "tokio", "toml", "tower", @@ -2456,8 +2449,8 @@ name = "pglt_lsp_converters" version = "0.0.0" dependencies = [ "anyhow", + "pglt_text_size", "rustc-hash 2.1.0", - "text-size", "tower-lsp", ] @@ -2479,7 +2472,7 @@ dependencies = [ "pglt_diagnostics", "pglt_lexer", "pglt_query_ext_codegen", - "text-size", + "pglt_text_size", ] [[package]] @@ -2524,8 +2517,8 @@ dependencies = [ "pglt_diagnostics", "pglt_lexer", "pglt_query_ext", + "pglt_text_size", "regex", - "text-size", ] [[package]] @@ -2556,10 +2549,20 @@ dependencies = [ name = "pglt_text_edit" version = "0.0.0" dependencies = [ + "pglt_text_size", "schemars", "serde", "similar", - "text-size", +] + +[[package]] +name = "pglt_text_size" +version = "0.0.0" +dependencies = [ + "schemars", + "serde", + "serde_test", + "static_assertions", ] [[package]] @@ -2589,8 +2592,8 @@ dependencies = [ "pglt_query_ext", "pglt_schema_cache", "pglt_test_utils", + "pglt_text_size", "sqlx", - "text-size", "tokio", "tree-sitter", "tree_sitter_sql", @@ -2614,13 +2617,13 @@ dependencies = [ "pglt_query_ext", "pglt_schema_cache", "pglt_statement_splitter", + "pglt_text_size", "pglt_typecheck", "rustc-hash 2.1.0", "serde", "serde_json", "sqlx", "tempfile", - "text-size", "tokio", "toml", "tracing", @@ -3300,6 +3303,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_test" +version = "1.0.177" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f901ee573cab6b3060453d2d5f0bae4e6d628c23c0a962ff9b5f1d7c8d4f1ed" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -3654,6 +3666,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "stringprep" version = "0.1.5" @@ -3757,15 +3775,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "text-size" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f18aa187839b2bdb1ad2fa35ead8c4c2976b64e4363c386d45ac0f7ee85c9233" -dependencies = [ - "serde", -] - [[package]] name = "thiserror" version = "1.0.69" diff --git a/Cargo.toml b/Cargo.toml index 6b8be0c5..face77a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ enumflags2 = "0.7.10" ignore = "0.4.23" indexmap = { version = "2.6.0", features = ["serde"] } insta = "1.31.0" -line_index = { path = "./lib/line_index", version = "0.0.0" } pg_query = "6.0.0" proc-macro2 = "1.0.66" quote = "1.0.33" @@ -39,7 +38,6 @@ smallvec = { version = "1.13.2", features = ["union", "const_new sqlx = { version = "0.8.2", features = ["runtime-async-std", "tls-rustls", "postgres", "json"] } syn = "1.0.109" termcolor = "1.4.1" -text-size = "1.1.1" tokio = { version = "1.40.0", features = ["full"] } toml = "0.8.19" tower-lsp = "0.20.0" @@ -75,6 +73,7 @@ pglt_query_proto_parser = { path = "./crates/pglt_query_proto_parser", versi pglt_schema_cache = { path = "./crates/pglt_schema_cache", version = "0.0.0" } pglt_statement_splitter = { path = "./crates/pglt_statement_splitter", version = "0.0.0" } pglt_text_edit = { path = "./crates/pglt_text_edit", version = "0.0.0" } +pglt_text_size = { path = "./crates/pglt_text_size", version = "0.0.0" } pglt_treesitter_queries = { path = "./crates/pglt_treesitter_queries", version = "0.0.0" } pglt_type_resolver = { path = "./crates/pglt_type_resolver", version = "0.0.0" } pglt_typecheck = { path = "./crates/pglt_typecheck", version = "0.0.0" } diff --git a/crates/pglt_analyse/Cargo.toml b/crates/pglt_analyse/Cargo.toml index e0b8f943..4574d90c 100644 --- a/crates/pglt_analyse/Cargo.toml +++ b/crates/pglt_analyse/Cargo.toml @@ -22,9 +22,9 @@ rustc-hash = { workspace = true } biome_deserialize = { workspace = true, optional = true } biome_deserialize_macros = { workspace = true, optional = true } enumflags2.workspace = true +pglt_text_size.workspace = true schemars = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"], optional = true } -text-size.workspace = true [features] serde = ["dep:serde", "dep:schemars", "dep:biome_deserialize", "dep:biome_deserialize_macros"] diff --git a/crates/pglt_analyse/src/rule.rs b/crates/pglt_analyse/src/rule.rs index 59950205..1f70cf84 100644 --- a/crates/pglt_analyse/src/rule.rs +++ b/crates/pglt_analyse/src/rule.rs @@ -5,9 +5,9 @@ use pglt_diagnostics::{ Advices, Category, Diagnostic, DiagnosticTags, Location, LogCategory, MessageAndDescription, Visit, }; +use pglt_text_size::TextRange; use std::cmp::Ordering; use std::fmt::Debug; -use text_size::TextRange; use crate::{categories::RuleCategory, context::RuleContext, registry::RegistryVisitor}; diff --git a/crates/pglt_commands/Cargo.toml b/crates/pglt_commands/Cargo.toml index ffa9c214..2507b914 100644 --- a/crates/pglt_commands/Cargo.toml +++ b/crates/pglt_commands/Cargo.toml @@ -12,10 +12,10 @@ version = "0.0.0" [dependencies] -anyhow = "1.0.62" -async-std = "1.12.0" -sqlx.workspace = true -text-size.workspace = true +anyhow = "1.0.62" +async-std = "1.12.0" +pglt_text_size.workspace = true +sqlx.workspace = true [lib] doctest = false diff --git a/crates/pglt_completions/Cargo.toml b/crates/pglt_completions/Cargo.toml index 84a8abd2..eec3d903 100644 --- a/crates/pglt_completions/Cargo.toml +++ b/crates/pglt_completions/Cargo.toml @@ -14,7 +14,7 @@ version = "0.0.0" [dependencies] async-std = "1.12.0" -text-size.workspace = true +pglt_text_size.workspace = true pglt_schema_cache.workspace = true diff --git a/crates/pglt_completions/src/complete.rs b/crates/pglt_completions/src/complete.rs index 6049a33a..1a1fd59e 100644 --- a/crates/pglt_completions/src/complete.rs +++ b/crates/pglt_completions/src/complete.rs @@ -1,5 +1,5 @@ +use pglt_text_size::TextSize; use serde::{Deserialize, Serialize}; -use text_size::TextSize; use crate::{ builder::CompletionBuilder, diff --git a/crates/pglt_configuration/Cargo.toml b/crates/pglt_configuration/Cargo.toml index 4c45bfbb..dde8592f 100644 --- a/crates/pglt_configuration/Cargo.toml +++ b/crates/pglt_configuration/Cargo.toml @@ -20,11 +20,11 @@ pglt_analyse = { workspace = true } pglt_analyser = { workspace = true } pglt_console = { workspace = true } pglt_diagnostics = { workspace = true } +pglt_text_size = { workspace = true } rustc-hash = { workspace = true } schemars = { workspace = true, features = ["indexmap1"], optional = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["raw_value"] } -text-size = { workspace = true } toml = { workspace = true } [lib] diff --git a/crates/pglt_console/Cargo.toml b/crates/pglt_console/Cargo.toml index fde900ec..ab4ce461 100644 --- a/crates/pglt_console/Cargo.toml +++ b/crates/pglt_console/Cargo.toml @@ -12,8 +12,8 @@ version = "0.0.0" [dependencies] -pglt_markup = { workspace = true } -text-size = { workspace = true } +pglt_markup = { workspace = true } +pglt_text_size = { workspace = true } schemars = { workspace = true, optional = true } serde = { workspace = true, optional = true, features = ["derive"] } diff --git a/crates/pglt_console/src/markup.rs b/crates/pglt_console/src/markup.rs index a6781c23..4c046ba8 100644 --- a/crates/pglt_console/src/markup.rs +++ b/crates/pglt_console/src/markup.rs @@ -4,8 +4,8 @@ use std::{ io, }; +use pglt_text_size::TextSize; use termcolor::{Color, ColorSpec}; -use text_size::TextSize; use crate::fmt::{Display, Formatter, MarkupElements, Write}; diff --git a/crates/pglt_diagnostics/Cargo.toml b/crates/pglt_diagnostics/Cargo.toml index 0f4d80e2..c477bfd7 100644 --- a/crates/pglt_diagnostics/Cargo.toml +++ b/crates/pglt_diagnostics/Cargo.toml @@ -19,11 +19,11 @@ pglt_console = { workspace = true, features = ["serde_markup"] } pglt_diagnostics_categories = { workspace = true, features = ["serde"] } pglt_diagnostics_macros = { workspace = true } pglt_text_edit = { workspace = true } +pglt_text_size.workspace = true schemars = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } termcolor = { workspace = true } -text-size.workspace = true unicode-width = { workspace = true } [features] diff --git a/crates/pglt_diagnostics/src/context.rs b/crates/pglt_diagnostics/src/context.rs index 01fa3448..f972a97b 100644 --- a/crates/pglt_diagnostics/src/context.rs +++ b/crates/pglt_diagnostics/src/context.rs @@ -251,7 +251,7 @@ mod internal { use pglt_console::{fmt, markup}; use pglt_text_edit::TextEdit; - use text_size::TextRange; + use pglt_text_size::TextRange; use crate::{ Advices, Backtrace, Category, Diagnostic, DiagnosticTags, LineIndex, LineIndexBuf, diff --git a/crates/pglt_diagnostics/src/display.rs b/crates/pglt_diagnostics/src/display.rs index b01a6e87..735d9e48 100644 --- a/crates/pglt_diagnostics/src/display.rs +++ b/crates/pglt_diagnostics/src/display.rs @@ -669,8 +669,8 @@ mod tests { use pglt_diagnostics::{DiagnosticTags, Severity}; use pglt_diagnostics_categories::{Category, category}; use pglt_text_edit::TextEdit; + use pglt_text_size::{TextRange, TextSize}; use serde_json::{from_value, json}; - use text_size::{TextRange, TextSize}; use crate::{self as pglt_diagnostics}; use crate::{ diff --git a/crates/pglt_diagnostics/src/display/frame.rs b/crates/pglt_diagnostics/src/display/frame.rs index 86ca21b0..53b0c6a0 100644 --- a/crates/pglt_diagnostics/src/display/frame.rs +++ b/crates/pglt_diagnostics/src/display/frame.rs @@ -7,7 +7,7 @@ use std::{ }; use pglt_console::{fmt, markup}; -use text_size::{TextLen, TextRange, TextSize}; +use pglt_text_size::{TextLen, TextRange, TextSize}; use unicode_width::UnicodeWidthChar; use crate::{ diff --git a/crates/pglt_diagnostics/src/display_github.rs b/crates/pglt_diagnostics/src/display_github.rs index 0b4a8e68..2e9d4b77 100644 --- a/crates/pglt_diagnostics/src/display_github.rs +++ b/crates/pglt_diagnostics/src/display_github.rs @@ -1,8 +1,8 @@ use crate::display::frame::SourceFile; use crate::{Diagnostic, Resource, Severity, diagnostic::internal::AsDiagnostic}; use pglt_console::{MarkupBuf, fmt, markup}; +use pglt_text_size::{TextRange, TextSize}; use std::io; -use text_size::{TextRange, TextSize}; /// Helper struct for printing a diagnostic as markup into any formatter /// implementing [pglt_console::fmt::Write]. diff --git a/crates/pglt_diagnostics/src/location.rs b/crates/pglt_diagnostics/src/location.rs index 4b9c8fe8..6678912d 100644 --- a/crates/pglt_diagnostics/src/location.rs +++ b/crates/pglt_diagnostics/src/location.rs @@ -1,8 +1,8 @@ +use pglt_text_size::{TextRange, TextSize}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; use std::ops::Range; use std::{borrow::Borrow, ops::Deref}; -use text_size::{TextRange, TextSize}; /// Represents the location of a diagnostic in a resource. #[derive(Debug, Default, Clone, Copy)] @@ -343,7 +343,7 @@ impl AsSourceCode for String { #[cfg(test)] mod tests { - use text_size::TextSize; + use pglt_text_size::TextSize; use super::LineIndexBuf; diff --git a/crates/pglt_diagnostics/src/serde.rs b/crates/pglt_diagnostics/src/serde.rs index 44eea25c..d564f832 100644 --- a/crates/pglt_diagnostics/src/serde.rs +++ b/crates/pglt_diagnostics/src/serde.rs @@ -2,11 +2,11 @@ use std::io; use pglt_console::{MarkupBuf, fmt, markup}; use pglt_text_edit::TextEdit; +use pglt_text_size::{TextRange, TextSize}; use serde::{ Deserialize, Deserializer, Serialize, Serializer, de::{self, SeqAccess}, }; -use text_size::{TextRange, TextSize}; use crate::{ Advices as _, Backtrace, Category, DiagnosticTags, LogCategory, Resource, Severity, SourceCode, @@ -358,8 +358,8 @@ impl<'de> Deserialize<'de> for DiagnosticTags { mod tests { use std::io; + use pglt_text_size::{TextRange, TextSize}; use serde_json::{Value, json}; - use text_size::{TextRange, TextSize}; use crate::{ self as pglt_diagnostics, {Advices, LogCategory, Visit}, diff --git a/crates/pglt_diagnostics/src/suggestion.rs b/crates/pglt_diagnostics/src/suggestion.rs index 0809b2bf..324d6197 100644 --- a/crates/pglt_diagnostics/src/suggestion.rs +++ b/crates/pglt_diagnostics/src/suggestion.rs @@ -1,7 +1,7 @@ use ::serde::{Deserialize, Serialize}; use pglt_console::MarkupBuf; use pglt_text_edit::TextEdit; -use text_size::TextRange; +use pglt_text_size::TextRange; /// Indicates how a tool should manage this suggestion. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] diff --git a/crates/pglt_lexer/Cargo.toml b/crates/pglt_lexer/Cargo.toml index e77e9a07..daffbf24 100644 --- a/crates/pglt_lexer/Cargo.toml +++ b/crates/pglt_lexer/Cargo.toml @@ -18,7 +18,7 @@ pg_query.workspace = true pglt_diagnostics.workspace = true pglt_lexer_codegen.workspace = true -text-size.workspace = true +pglt_text_size.workspace = true [dev-dependencies] insta.workspace = true diff --git a/crates/pglt_lexer/src/diagnostics.rs b/crates/pglt_lexer/src/diagnostics.rs index 29cd72cd..f1968437 100644 --- a/crates/pglt_lexer/src/diagnostics.rs +++ b/crates/pglt_lexer/src/diagnostics.rs @@ -1,5 +1,5 @@ use pglt_diagnostics::{Diagnostic, MessageAndDescription}; -use text_size::TextRange; +use pglt_text_size::TextRange; /// A specialized diagnostic for scan errors. /// diff --git a/crates/pglt_lexer/src/lib.rs b/crates/pglt_lexer/src/lib.rs index 3c4ed2af..696d22e2 100644 --- a/crates/pglt_lexer/src/lib.rs +++ b/crates/pglt_lexer/src/lib.rs @@ -3,9 +3,9 @@ pub mod diagnostics; use diagnostics::ScanError; use pg_query::protobuf::{KeywordKind, ScanToken}; +use pglt_text_size::{TextLen, TextRange, TextSize}; use regex::Regex; use std::{collections::VecDeque, sync::LazyLock}; -use text_size::{TextLen, TextRange, TextSize}; pub use crate::codegen::SyntaxKind; diff --git a/crates/pglt_lsp/Cargo.toml b/crates/pglt_lsp/Cargo.toml index 2a21e08c..ddef9f9a 100644 --- a/crates/pglt_lsp/Cargo.toml +++ b/crates/pglt_lsp/Cargo.toml @@ -12,25 +12,25 @@ version = "0.0.0" [dependencies] -anyhow = { workspace = true } -biome_deserialize = { workspace = true } -futures = "0.3.31" -pglt_analyse = { workspace = true } -pglt_completions = { workspace = true } -pglt_configuration = { workspace = true } -pglt_console = { workspace = true } -pglt_diagnostics = { workspace = true } -pglt_fs = { workspace = true } -pglt_lsp_converters = { workspace = true } -pglt_text_edit = { workspace = true } -pglt_workspace = { workspace = true } -rustc-hash = { workspace = true } -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true } -text-size.workspace = true -tokio = { workspace = true, features = ["rt", "io-std"] } -tower-lsp = { version = "0.20.0" } -tracing = { workspace = true, features = ["attributes"] } +anyhow = { workspace = true } +biome_deserialize = { workspace = true } +futures = "0.3.31" +pglt_analyse = { workspace = true } +pglt_completions = { workspace = true } +pglt_configuration = { workspace = true } +pglt_console = { workspace = true } +pglt_diagnostics = { workspace = true } +pglt_fs = { workspace = true } +pglt_lsp_converters = { workspace = true } +pglt_text_edit = { workspace = true } +pglt_text_size.workspace = true +pglt_workspace = { workspace = true } +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] pglt_test_utils = { workspace = true } diff --git a/crates/pglt_lsp/src/utils.rs b/crates/pglt_lsp/src/utils.rs index 1dc07f91..d1211b6c 100644 --- a/crates/pglt_lsp/src/utils.rs +++ b/crates/pglt_lsp/src/utils.rs @@ -7,12 +7,12 @@ use pglt_diagnostics::{Diagnostic, DiagnosticTags, Location, PrintDescription, S use pglt_lsp_converters::line_index::LineIndex; use pglt_lsp_converters::{PositionEncoding, from_proto, to_proto}; use pglt_text_edit::{CompressedOp, DiffOp, TextEdit}; +use pglt_text_size::{TextRange, TextSize}; use std::any::Any; use std::borrow::Cow; use std::fmt::{Debug, Display}; use std::io; use std::ops::{Add, Range}; -use text_size::{TextRange, TextSize}; use tower_lsp::jsonrpc::Error as LspError; use tower_lsp::lsp_types; use tower_lsp::lsp_types::{self as lsp, CodeDescription, Url}; diff --git a/crates/pglt_lsp_converters/Cargo.toml b/crates/pglt_lsp_converters/Cargo.toml index 48687ba3..c8bace04 100644 --- a/crates/pglt_lsp_converters/Cargo.toml +++ b/crates/pglt_lsp_converters/Cargo.toml @@ -12,10 +12,10 @@ version = "0.0.0" [dependencies] -anyhow = { workspace = true } -rustc-hash = { workspace = true } -text-size.workspace = true -tower-lsp = { version = "0.20.0" } +anyhow = { workspace = true } +pglt_text_size.workspace = true +rustc-hash = { workspace = true } +tower-lsp = { version = "0.20.0" } [dev-dependencies] diff --git a/crates/pglt_lsp_converters/src/from_proto.rs b/crates/pglt_lsp_converters/src/from_proto.rs index 1be89337..464a0934 100644 --- a/crates/pglt_lsp_converters/src/from_proto.rs +++ b/crates/pglt_lsp_converters/src/from_proto.rs @@ -1,7 +1,7 @@ use crate::line_index::LineIndex; use crate::{LineCol, PositionEncoding, WideLineCol}; use anyhow::{Context, Result}; -use text_size::{TextRange, TextSize}; +use pglt_text_size::{TextRange, TextSize}; use tower_lsp::lsp_types; /// The function is used to convert a LSP position to TextSize. diff --git a/crates/pglt_lsp_converters/src/lib.rs b/crates/pglt_lsp_converters/src/lib.rs index 64aa31cb..284114f7 100644 --- a/crates/pglt_lsp_converters/src/lib.rs +++ b/crates/pglt_lsp_converters/src/lib.rs @@ -1,6 +1,6 @@ //! The crate contains a set of converters to translate between `lsp-types` and `text_size` (and vice versa) types. -use text_size::TextSize; +use pglt_text_size::TextSize; use tower_lsp::lsp_types::{ClientCapabilities, PositionEncodingKind}; pub mod from_proto; @@ -91,7 +91,7 @@ mod tests { use crate::line_index::LineIndex; use crate::to_proto::position; use crate::{LineCol, PositionEncoding, WideEncoding}; - use text_size::TextSize; + use pglt_text_size::TextSize; use tower_lsp::lsp_types::Position; macro_rules! check_conversion { diff --git a/crates/pglt_lsp_converters/src/line_index.rs b/crates/pglt_lsp_converters/src/line_index.rs index 50376566..ffd77bdf 100644 --- a/crates/pglt_lsp_converters/src/line_index.rs +++ b/crates/pglt_lsp_converters/src/line_index.rs @@ -3,8 +3,8 @@ use std::mem; +use pglt_text_size::TextSize; use rustc_hash::FxHashMap; -use text_size::TextSize; use crate::{LineCol, WideChar, WideEncoding, WideLineCol}; diff --git a/crates/pglt_lsp_converters/src/to_proto.rs b/crates/pglt_lsp_converters/src/to_proto.rs index 387abbb5..f4446972 100644 --- a/crates/pglt_lsp_converters/src/to_proto.rs +++ b/crates/pglt_lsp_converters/src/to_proto.rs @@ -1,7 +1,7 @@ use crate::PositionEncoding; use crate::line_index::LineIndex; use anyhow::{Context, Result}; -use text_size::{TextRange, TextSize}; +use pglt_text_size::{TextRange, TextSize}; use tower_lsp::lsp_types; /// The function is used to convert TextSize to a LSP position. diff --git a/crates/pglt_query_ext/Cargo.toml b/crates/pglt_query_ext/Cargo.toml index 99e94ee6..2c97a9f9 100644 --- a/crates/pglt_query_ext/Cargo.toml +++ b/crates/pglt_query_ext/Cargo.toml @@ -18,7 +18,7 @@ pg_query.workspace = true pglt_diagnostics.workspace = true pglt_lexer.workspace = true pglt_query_ext_codegen.workspace = true -text-size.workspace = true +pglt_text_size.workspace = true [lib] doctest = false diff --git a/crates/pglt_query_ext/src/diagnostics.rs b/crates/pglt_query_ext/src/diagnostics.rs index a091dc97..68879064 100644 --- a/crates/pglt_query_ext/src/diagnostics.rs +++ b/crates/pglt_query_ext/src/diagnostics.rs @@ -1,5 +1,5 @@ use pglt_diagnostics::{Diagnostic, MessageAndDescription}; -use text_size::TextRange; +use pglt_text_size::TextRange; /// A specialized diagnostic for the libpg_query parser. /// diff --git a/crates/pglt_statement_splitter/Cargo.toml b/crates/pglt_statement_splitter/Cargo.toml index 3148d4f6..9fbc5fc9 100644 --- a/crates/pglt_statement_splitter/Cargo.toml +++ b/crates/pglt_statement_splitter/Cargo.toml @@ -15,8 +15,8 @@ version = "0.0.0" pglt_diagnostics = { workspace = true } pglt_lexer.workspace = true pglt_query_ext.workspace = true +pglt_text_size.workspace = true regex.workspace = true -text-size.workspace = true [dev-dependencies] ntest = "0.9.3" diff --git a/crates/pglt_statement_splitter/src/diagnostics.rs b/crates/pglt_statement_splitter/src/diagnostics.rs index 5288e08d..823b6ae7 100644 --- a/crates/pglt_statement_splitter/src/diagnostics.rs +++ b/crates/pglt_statement_splitter/src/diagnostics.rs @@ -1,5 +1,5 @@ use pglt_diagnostics::{Diagnostic, MessageAndDescription}; -use text_size::TextRange; +use pglt_text_size::TextRange; /// A specialized diagnostic for the statement splitter parser. /// diff --git a/crates/pglt_statement_splitter/src/lib.rs b/crates/pglt_statement_splitter/src/lib.rs index 7d74db03..0f5cc45c 100644 --- a/crates/pglt_statement_splitter/src/lib.rs +++ b/crates/pglt_statement_splitter/src/lib.rs @@ -22,7 +22,7 @@ mod tests { use diagnostics::SplitDiagnostic; use ntest::timeout; use pglt_lexer::SyntaxKind; - use text_size::TextRange; + use pglt_text_size::TextRange; use super::*; diff --git a/crates/pglt_statement_splitter/src/parser.rs b/crates/pglt_statement_splitter/src/parser.rs index f89f28dd..1252d2bd 100644 --- a/crates/pglt_statement_splitter/src/parser.rs +++ b/crates/pglt_statement_splitter/src/parser.rs @@ -6,7 +6,7 @@ mod dml; pub use common::source; use pglt_lexer::{SyntaxKind, Token, WHITESPACE_TOKENS}; -use text_size::{TextRange, TextSize}; +use pglt_text_size::{TextRange, TextSize}; use crate::diagnostics::SplitDiagnostic; diff --git a/crates/pglt_text_edit/Cargo.toml b/crates/pglt_text_edit/Cargo.toml index 1025093b..01bc0bbb 100644 --- a/crates/pglt_text_edit/Cargo.toml +++ b/crates/pglt_text_edit/Cargo.toml @@ -12,10 +12,10 @@ version = "0.0.0" [dependencies] -schemars = { workspace = true, optional = true } -serde = { workspace = true, features = ["derive"] } -similar = { workspace = true, features = ["unicode"] } -text-size = { workspace = true, features = ["serde"] } +pglt_text_size = { workspace = true, features = ["serde"] } +schemars = { workspace = true, optional = true } +serde = { workspace = true, features = ["derive"] } +similar = { workspace = true, features = ["unicode"] } [features] schemars = ["dep:schemars"] diff --git a/crates/pglt_text_edit/src/lib.rs b/crates/pglt_text_edit/src/lib.rs index 116d7066..185bc1ca 100644 --- a/crates/pglt_text_edit/src/lib.rs +++ b/crates/pglt_text_edit/src/lib.rs @@ -10,10 +10,10 @@ use std::{cmp::Ordering, num::NonZeroU32}; +use pglt_text_size::{TextRange, TextSize}; use serde::{Deserialize, Serialize}; pub use similar::ChangeTag; use similar::{TextDiff, utils::TextDiffRemapper}; -use text_size::{TextRange, TextSize}; #[derive(Default, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] diff --git a/crates/pglt_text_size/Cargo.toml b/crates/pglt_text_size/Cargo.toml new file mode 100644 index 00000000..ec5ce81f --- /dev/null +++ b/crates/pglt_text_size/Cargo.toml @@ -0,0 +1,28 @@ +[package] +authors.workspace = true +categories.workspace = true +description = "" +edition.workspace = true +homepage.workspace = true +keywords.workspace = true +license.workspace = true +name = "pglt_text_size" +repository.workspace = true +version = "0.0.0" + +[dependencies] +schemars = { workspace = true, optional = true } +serde = { workspace = true, optional = true } + +[features] +schema = ["dep:schemars", "serde"] +serde = ["dep:serde"] + +[dev-dependencies] +serde_test = "1.0" +static_assertions = "1.1" + +[[test]] +name = "serde" +path = "tests/serde.rs" +required-features = ["serde"] diff --git a/crates/pglt_text_size/src/lib.rs b/crates/pglt_text_size/src/lib.rs new file mode 100644 index 00000000..92bd36b1 --- /dev/null +++ b/crates/pglt_text_size/src/lib.rs @@ -0,0 +1,32 @@ +//! Newtypes for working with text sizes/ranges in a more type-safe manner. +//! +//! This library can help with two things: +//! * Reducing storage requirements for offsets and ranges, under the +//! assumption that 32 bits is enough. +//! * Providing standard vocabulary types for applications where text ranges +//! are pervasive. +//! +//! However, you should not use this library simply because you work with +//! strings. In the overwhelming majority of cases, using `usize` and +//! `std::ops::Range` is better. In particular, if you are publishing a +//! library, using only std types in the interface would make it more +//! interoperable. Similarly, if you are writing something like a lexer, which +//! produces, but does not *store* text ranges, then sticking to `usize` would +//! be better. +//! +//! Minimal Supported Rust Version: latest stable. + +#![forbid(unsafe_code)] +#![warn(missing_debug_implementations, missing_docs)] + +mod range; +mod size; +mod traits; + +#[cfg(feature = "serde")] +mod serde_impls; + +pub use crate::{range::TextRange, size::TextSize, traits::TextLen}; + +#[cfg(target_pointer_width = "16")] +compile_error!("text-size assumes usize >= u32 and does not work on 16-bit targets"); diff --git a/crates/pglt_text_size/src/range.rs b/crates/pglt_text_size/src/range.rs new file mode 100644 index 00000000..028e5911 --- /dev/null +++ b/crates/pglt_text_size/src/range.rs @@ -0,0 +1,456 @@ +use cmp::Ordering; + +use { + crate::TextSize, + std::{ + cmp, fmt, + ops::{Add, AddAssign, Bound, Index, IndexMut, Range, RangeBounds, Sub, SubAssign}, + }, +}; + +/// A range in text, represented as a pair of [`TextSize`][struct@TextSize]. +/// +/// It is a logic error for `start` to be greater than `end`. +#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)] +pub struct TextRange { + // Invariant: start <= end + start: TextSize, + end: TextSize, +} + +impl fmt::Debug for TextRange { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}..{}", self.start().raw, self.end().raw) + } +} + +impl TextRange { + /// Creates a new `TextRange` with the given `start` and `end` (`start..end`). + /// + /// # Panics + /// + /// Panics if `end < start`. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let start = TextSize::from(5); + /// let end = TextSize::from(10); + /// let range = TextRange::new(start, end); + /// + /// assert_eq!(range.start(), start); + /// assert_eq!(range.end(), end); + /// assert_eq!(range.len(), end - start); + /// ``` + #[inline] + pub const fn new(start: TextSize, end: TextSize) -> TextRange { + assert!(start.raw <= end.raw); + TextRange { start, end } + } + + /// Create a new `TextRange` with the given `offset` and `len` (`offset..offset + len`). + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let text = "0123456789"; + /// + /// let offset = TextSize::from(2); + /// let length = TextSize::from(5); + /// let range = TextRange::at(offset, length); + /// + /// assert_eq!(range, TextRange::new(offset, offset + length)); + /// assert_eq!(&text[range], "23456") + /// ``` + #[inline] + pub const fn at(offset: TextSize, len: TextSize) -> TextRange { + TextRange::new(offset, TextSize::new(offset.raw + len.raw)) + } + + /// Create a zero-length range at the specified offset (`offset..offset`). + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let point: TextSize; + /// # point = TextSize::from(3); + /// let range = TextRange::empty(point); + /// assert!(range.is_empty()); + /// assert_eq!(range, TextRange::new(point, point)); + /// ``` + #[inline] + pub const fn empty(offset: TextSize) -> TextRange { + TextRange { + start: offset, + end: offset, + } + } + + /// Create a range up to the given end (`..end`). + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let point: TextSize; + /// # point = TextSize::from(12); + /// let range = TextRange::up_to(point); + /// + /// assert_eq!(range.len(), point); + /// assert_eq!(range, TextRange::new(0.into(), point)); + /// assert_eq!(range, TextRange::at(0.into(), point)); + /// ``` + #[inline] + pub const fn up_to(end: TextSize) -> TextRange { + TextRange { + start: TextSize::new(0), + end, + } + } +} + +/// Identity methods. +impl TextRange { + /// The start point of this range. + #[inline] + pub const fn start(self) -> TextSize { + self.start + } + + /// The end point of this range. + #[inline] + pub const fn end(self) -> TextSize { + self.end + } + + /// The size of this range. + #[inline] + pub const fn len(self) -> TextSize { + // HACK for const fn: math on primitives only + TextSize { + raw: self.end().raw - self.start().raw, + } + } + + /// Check if this range is empty. + #[inline] + pub const fn is_empty(self) -> bool { + // HACK for const fn: math on primitives only + self.start().raw == self.end().raw + } +} + +/// Manipulation methods. +impl TextRange { + /// Check if this range contains an offset. + /// + /// The end index is considered excluded. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let (start, end): (TextSize, TextSize); + /// # start = 10.into(); end = 20.into(); + /// let range = TextRange::new(start, end); + /// assert!(range.contains(start)); + /// assert!(!range.contains(end)); + /// ``` + #[inline] + pub fn contains(self, offset: TextSize) -> bool { + self.start() <= offset && offset < self.end() + } + + /// Check if this range contains an offset. + /// + /// The end index is considered included. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let (start, end): (TextSize, TextSize); + /// # start = 10.into(); end = 20.into(); + /// let range = TextRange::new(start, end); + /// assert!(range.contains_inclusive(start)); + /// assert!(range.contains_inclusive(end)); + /// ``` + #[inline] + pub fn contains_inclusive(self, offset: TextSize) -> bool { + self.start() <= offset && offset <= self.end() + } + + /// Check if this range completely contains another range. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let larger = TextRange::new(0.into(), 20.into()); + /// let smaller = TextRange::new(5.into(), 15.into()); + /// assert!(larger.contains_range(smaller)); + /// assert!(!smaller.contains_range(larger)); + /// + /// // a range always contains itself + /// assert!(larger.contains_range(larger)); + /// assert!(smaller.contains_range(smaller)); + /// ``` + #[inline] + pub fn contains_range(self, other: TextRange) -> bool { + self.start() <= other.start() && other.end() <= self.end() + } + + /// The range covered by both ranges, if it exists. + /// If the ranges touch but do not overlap, the output range is empty. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// assert_eq!( + /// TextRange::intersect( + /// TextRange::new(0.into(), 10.into()), + /// TextRange::new(5.into(), 15.into()), + /// ), + /// Some(TextRange::new(5.into(), 10.into())), + /// ); + /// ``` + #[inline] + pub fn intersect(self, other: TextRange) -> Option { + let start = cmp::max(self.start(), other.start()); + let end = cmp::min(self.end(), other.end()); + if end < start { + return None; + } + Some(TextRange::new(start, end)) + } + + /// Extends the range to cover `other` as well. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// assert_eq!( + /// TextRange::cover( + /// TextRange::new(0.into(), 5.into()), + /// TextRange::new(15.into(), 20.into()), + /// ), + /// TextRange::new(0.into(), 20.into()), + /// ); + /// ``` + #[inline] + pub fn cover(self, other: TextRange) -> TextRange { + let start = cmp::min(self.start(), other.start()); + let end = cmp::max(self.end(), other.end()); + TextRange::new(start, end) + } + + /// Extends the range to cover `other` offsets as well. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// assert_eq!( + /// TextRange::empty(0.into()).cover_offset(20.into()), + /// TextRange::new(0.into(), 20.into()), + /// ) + /// ``` + #[inline] + pub fn cover_offset(self, offset: TextSize) -> TextRange { + self.cover(TextRange::empty(offset)) + } + + /// Add an offset to this range. + /// + /// Note that this is not appropriate for changing where a `TextRange` is + /// within some string; rather, it is for changing the reference anchor + /// that the `TextRange` is measured against. + /// + /// The unchecked version (`Add::add`) will _always_ panic on overflow, + /// in contrast to primitive integers, which check in debug mode only. + #[inline] + pub fn checked_add(self, offset: TextSize) -> Option { + Some(TextRange { + start: self.start.checked_add(offset)?, + end: self.end.checked_add(offset)?, + }) + } + + /// Subtract an offset from this range. + /// + /// Note that this is not appropriate for changing where a `TextRange` is + /// within some string; rather, it is for changing the reference anchor + /// that the `TextRange` is measured against. + /// + /// The unchecked version (`Sub::sub`) will _always_ panic on overflow, + /// in contrast to primitive integers, which check in debug mode only. + #[inline] + pub fn checked_sub(self, offset: TextSize) -> Option { + Some(TextRange { + start: self.start.checked_sub(offset)?, + end: self.end.checked_sub(offset)?, + }) + } + + /// Relative order of the two ranges (overlapping ranges are considered + /// equal). + /// + /// + /// This is useful when, for example, binary searching an array of disjoint + /// ranges. + /// + /// # Examples + /// + /// ``` + /// # use pglt_text_size::*; + /// # use std::cmp::Ordering; + /// + /// let a = TextRange::new(0.into(), 3.into()); + /// let b = TextRange::new(4.into(), 5.into()); + /// assert_eq!(a.ordering(b), Ordering::Less); + /// + /// let a = TextRange::new(0.into(), 3.into()); + /// let b = TextRange::new(3.into(), 5.into()); + /// assert_eq!(a.ordering(b), Ordering::Less); + /// + /// let a = TextRange::new(0.into(), 3.into()); + /// let b = TextRange::new(2.into(), 5.into()); + /// assert_eq!(a.ordering(b), Ordering::Equal); + /// + /// let a = TextRange::new(0.into(), 3.into()); + /// let b = TextRange::new(2.into(), 2.into()); + /// assert_eq!(a.ordering(b), Ordering::Equal); + /// + /// let a = TextRange::new(2.into(), 3.into()); + /// let b = TextRange::new(2.into(), 2.into()); + /// assert_eq!(a.ordering(b), Ordering::Greater); + /// ``` + #[inline] + pub fn ordering(self, other: TextRange) -> Ordering { + if self.end() <= other.start() { + Ordering::Less + } else if other.end() <= self.start() { + Ordering::Greater + } else { + Ordering::Equal + } + } +} + +impl Index for str { + type Output = str; + #[inline] + fn index(&self, index: TextRange) -> &str { + &self[Range::::from(index)] + } +} + +impl Index for String { + type Output = str; + #[inline] + fn index(&self, index: TextRange) -> &str { + &self[Range::::from(index)] + } +} + +impl IndexMut for str { + #[inline] + fn index_mut(&mut self, index: TextRange) -> &mut str { + &mut self[Range::::from(index)] + } +} + +impl IndexMut for String { + #[inline] + fn index_mut(&mut self, index: TextRange) -> &mut str { + &mut self[Range::::from(index)] + } +} + +impl RangeBounds for TextRange { + fn start_bound(&self) -> Bound<&TextSize> { + Bound::Included(&self.start) + } + + fn end_bound(&self) -> Bound<&TextSize> { + Bound::Excluded(&self.end) + } +} + +impl From for Range +where + T: From, +{ + #[inline] + fn from(r: TextRange) -> Self { + r.start().into()..r.end().into() + } +} + +macro_rules! ops { + (impl $Op:ident for TextRange by fn $f:ident = $op:tt) => { + impl $Op<&TextSize> for TextRange { + type Output = TextRange; + #[inline] + fn $f(self, other: &TextSize) -> TextRange { + self $op *other + } + } + impl $Op for &TextRange + where + TextRange: $Op, + { + type Output = TextRange; + #[inline] + fn $f(self, other: T) -> TextRange { + *self $op other + } + } + }; +} + +impl Add for TextRange { + type Output = TextRange; + #[inline] + fn add(self, offset: TextSize) -> TextRange { + self.checked_add(offset) + .expect("TextRange +offset overflowed") + } +} + +impl Sub for TextRange { + type Output = TextRange; + #[inline] + fn sub(self, offset: TextSize) -> TextRange { + self.checked_sub(offset) + .expect("TextRange -offset overflowed") + } +} + +ops!(impl Add for TextRange by fn add = +); +ops!(impl Sub for TextRange by fn sub = -); + +impl AddAssign for TextRange +where + TextRange: Add, +{ + #[inline] + fn add_assign(&mut self, rhs: A) { + *self = *self + rhs + } +} + +impl SubAssign for TextRange +where + TextRange: Sub, +{ + #[inline] + fn sub_assign(&mut self, rhs: S) { + *self = *self - rhs + } +} diff --git a/crates/pglt_text_size/src/schemars_impls.rs b/crates/pglt_text_size/src/schemars_impls.rs new file mode 100644 index 00000000..8c216ccd --- /dev/null +++ b/crates/pglt_text_size/src/schemars_impls.rs @@ -0,0 +1,32 @@ +//! This module implements the [JsonSchema] trait from the [schemars] crate for +//! [TextSize] and [TextRange] if the `schemars` feature is enabled. This trait +//! exposes meta-information on how a given type is serialized and deserialized +//! using `serde`, and is currently used to generate TypeScript types for the node.js and wasm +//! bindings to the Workspace API + +use crate::{TextRange, TextSize}; +use schemars::{JsonSchema, r#gen::SchemaGenerator, schema::Schema}; + +impl JsonSchema for TextSize { + fn schema_name() -> String { + String::from("TextSize") + } + + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + // TextSize is represented as a raw u32, see serde_impls.rs for the + // actual implementation + ::json_schema(generator) + } +} + +impl JsonSchema for TextRange { + fn schema_name() -> String { + String::from("TextRange") + } + + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + // TextSize is represented as (TextSize, TextSize), see serde_impls.rs + // for the actual implementation + <(TextSize, TextSize)>::json_schema(generator) + } +} diff --git a/crates/pglt_text_size/src/serde_impls.rs b/crates/pglt_text_size/src/serde_impls.rs new file mode 100644 index 00000000..a422c75f --- /dev/null +++ b/crates/pglt_text_size/src/serde_impls.rs @@ -0,0 +1,48 @@ +use { + crate::{TextRange, TextSize}, + serde::{Deserialize, Deserializer, Serialize, Serializer, de}, +}; + +impl Serialize for TextSize { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.raw.serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for TextSize { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + u32::deserialize(deserializer).map(TextSize::from) + } +} + +impl Serialize for TextRange { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + (self.start(), self.end()).serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for TextRange { + #[allow(clippy::nonminimal_bool)] + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let (start, end) = Deserialize::deserialize(deserializer)?; + if !(start <= end) { + return Err(de::Error::custom(format!( + "invalid range: {:?}..{:?}", + start, end + ))); + } + Ok(TextRange::new(start, end)) + } +} diff --git a/crates/pglt_text_size/src/size.rs b/crates/pglt_text_size/src/size.rs new file mode 100644 index 00000000..658cb13a --- /dev/null +++ b/crates/pglt_text_size/src/size.rs @@ -0,0 +1,173 @@ +use { + crate::TextLen, + std::{ + convert::TryFrom, + fmt, iter, + num::TryFromIntError, + ops::{Add, AddAssign, Sub, SubAssign}, + u32, + }, +}; + +/// A measure of text length. Also, equivalently, an index into text. +/// +/// This is a UTF-8 bytes offset stored as `u32`, but +/// most clients should treat it as an opaque measure. +/// +/// For cases that need to escape `TextSize` and return to working directly +/// with primitive integers, `TextSize` can be converted losslessly to/from +/// `u32` via [`From`] conversions as well as losslessly be converted [`Into`] +/// `usize`. The `usize -> TextSize` direction can be done via [`TryFrom`]. +/// +/// These escape hatches are primarily required for unit testing and when +/// converting from UTF-8 size to another coordinate space, such as UTF-16. +#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct TextSize { + pub(crate) raw: u32, +} + +impl fmt::Debug for TextSize { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.raw) + } +} + +impl TextSize { + /// Creates a new instance of `TextSize` from a raw `u32`. + #[inline] + pub const fn new(raw: u32) -> TextSize { + TextSize { raw } + } + + /// The text size of some primitive text-like object. + /// + /// Accepts `char`, `&str`, and `&String`. + /// + /// # Examples + /// + /// ```rust + /// # use pglt_text_size::*; + /// let char_size = TextSize::of('🦀'); + /// assert_eq!(char_size, TextSize::from(4)); + /// + /// let str_size = TextSize::of("rust-analyzer"); + /// assert_eq!(str_size, TextSize::from(13)); + /// ``` + #[inline] + pub fn of(text: T) -> TextSize { + text.text_len() + } +} + +/// Methods to act like a primitive integer type, where reasonably applicable. +// Last updated for parity with Rust 1.42.0. +impl TextSize { + /// Checked addition. Returns `None` if overflow occurred. + #[inline] + pub const fn checked_add(self, rhs: TextSize) -> Option { + match self.raw.checked_add(rhs.raw) { + Some(raw) => Some(TextSize { raw }), + None => None, + } + } + + /// Checked subtraction. Returns `None` if overflow occurred. + #[inline] + pub const fn checked_sub(self, rhs: TextSize) -> Option { + match self.raw.checked_sub(rhs.raw) { + Some(raw) => Some(TextSize { raw }), + None => None, + } + } +} + +impl From for TextSize { + #[inline] + fn from(raw: u32) -> Self { + TextSize { raw } + } +} + +impl From for u32 { + #[inline] + fn from(value: TextSize) -> Self { + value.raw + } +} + +impl TryFrom for TextSize { + type Error = TryFromIntError; + #[inline] + fn try_from(value: usize) -> Result { + Ok(u32::try_from(value)?.into()) + } +} + +impl From for usize { + #[inline] + fn from(value: TextSize) -> Self { + value.raw as usize + } +} + +macro_rules! ops { + (impl $Op:ident for TextSize by fn $f:ident = $op:tt) => { + impl $Op for TextSize { + type Output = TextSize; + #[inline] + fn $f(self, other: TextSize) -> TextSize { + TextSize { raw: self.raw $op other.raw } + } + } + impl $Op<&TextSize> for TextSize { + type Output = TextSize; + #[inline] + fn $f(self, other: &TextSize) -> TextSize { + self $op *other + } + } + impl $Op for &TextSize + where + TextSize: $Op, + { + type Output = TextSize; + #[inline] + fn $f(self, other: T) -> TextSize { + *self $op other + } + } + }; +} + +ops!(impl Add for TextSize by fn add = +); +ops!(impl Sub for TextSize by fn sub = -); + +impl AddAssign for TextSize +where + TextSize: Add, +{ + #[inline] + fn add_assign(&mut self, rhs: A) { + *self = *self + rhs + } +} + +impl SubAssign for TextSize +where + TextSize: Sub, +{ + #[inline] + fn sub_assign(&mut self, rhs: S) { + *self = *self - rhs + } +} + +impl iter::Sum for TextSize +where + TextSize: Add, +{ + #[inline] + fn sum>(iter: I) -> TextSize { + iter.fold(0.into(), Add::add) + } +} diff --git a/crates/pglt_text_size/src/traits.rs b/crates/pglt_text_size/src/traits.rs new file mode 100644 index 00000000..d0bb6c1f --- /dev/null +++ b/crates/pglt_text_size/src/traits.rs @@ -0,0 +1,36 @@ +use {crate::TextSize, std::convert::TryInto}; + +use priv_in_pub::Sealed; +mod priv_in_pub { + pub trait Sealed {} +} + +/// Primitives with a textual length that can be passed to [`TextSize::of`]. +pub trait TextLen: Copy + Sealed { + /// The textual length of this primitive. + fn text_len(self) -> TextSize; +} + +impl Sealed for &'_ str {} +impl TextLen for &'_ str { + #[inline] + fn text_len(self) -> TextSize { + self.len().try_into().unwrap() + } +} + +impl Sealed for &'_ String {} +impl TextLen for &'_ String { + #[inline] + fn text_len(self) -> TextSize { + self.as_str().text_len() + } +} + +impl Sealed for char {} +impl TextLen for char { + #[inline] + fn text_len(self) -> TextSize { + (self.len_utf8() as u32).into() + } +} diff --git a/crates/pglt_text_size/tests/auto_traits.rs b/crates/pglt_text_size/tests/auto_traits.rs new file mode 100644 index 00000000..0a64ba15 --- /dev/null +++ b/crates/pglt_text_size/tests/auto_traits.rs @@ -0,0 +1,18 @@ +use { + pglt_text_size::*, + static_assertions::*, + std::{ + fmt::Debug, + hash::Hash, + marker::{Send, Sync}, + panic::{RefUnwindSafe, UnwindSafe}, + }, +}; + +// auto traits +assert_impl_all!(TextSize: Send, Sync, Unpin, UnwindSafe, RefUnwindSafe); +assert_impl_all!(TextRange: Send, Sync, Unpin, UnwindSafe, RefUnwindSafe); + +// common traits +assert_impl_all!(TextSize: Copy, Debug, Default, Hash, Ord); +assert_impl_all!(TextRange: Copy, Debug, Default, Hash, Eq); diff --git a/crates/pglt_text_size/tests/constructors.rs b/crates/pglt_text_size/tests/constructors.rs new file mode 100644 index 00000000..ea4a4064 --- /dev/null +++ b/crates/pglt_text_size/tests/constructors.rs @@ -0,0 +1,24 @@ +use pglt_text_size::TextSize; + +#[derive(Copy, Clone)] +struct BadRope<'a>(&'a [&'a str]); + +impl BadRope<'_> { + fn text_len(self) -> TextSize { + self.0.iter().copied().map(TextSize::of).sum() + } +} + +#[test] +fn main() { + let x: char = 'c'; + let _ = TextSize::of(x); + + let x: &str = "hello"; + let _ = TextSize::of(x); + + let x: &String = &"hello".into(); + let _ = TextSize::of(x); + + let _ = BadRope(&[""]).text_len(); +} diff --git a/crates/pglt_text_size/tests/indexing.rs b/crates/pglt_text_size/tests/indexing.rs new file mode 100644 index 00000000..bb25ee62 --- /dev/null +++ b/crates/pglt_text_size/tests/indexing.rs @@ -0,0 +1,8 @@ +use pglt_text_size::*; + +#[test] +fn main() { + let range = TextRange::default(); + _ = &""[range]; + _ = &String::new()[range]; +} diff --git a/crates/pglt_text_size/tests/main.rs b/crates/pglt_text_size/tests/main.rs new file mode 100644 index 00000000..6b9fe923 --- /dev/null +++ b/crates/pglt_text_size/tests/main.rs @@ -0,0 +1,76 @@ +use {pglt_text_size::*, std::ops}; + +fn size(x: u32) -> TextSize { + TextSize::from(x) +} + +fn range(x: ops::Range) -> TextRange { + TextRange::new(x.start.into(), x.end.into()) +} + +#[test] +fn sum() { + let xs: Vec = vec![size(0), size(1), size(2)]; + assert_eq!(xs.iter().sum::(), size(3)); + assert_eq!(xs.into_iter().sum::(), size(3)); +} + +#[test] +fn math() { + assert_eq!(size(10) + size(5), size(15)); + assert_eq!(size(10) - size(5), size(5)); +} + +#[test] +fn checked_math() { + assert_eq!(size(1).checked_add(size(1)), Some(size(2))); + assert_eq!(size(1).checked_sub(size(1)), Some(size(0))); + assert_eq!(size(1).checked_sub(size(2)), None); + assert_eq!(size(!0).checked_add(size(1)), None); +} + +#[test] +#[rustfmt::skip] +fn contains() { + assert!( range(2..4).contains_range(range(2..3))); + assert!( ! range(2..4).contains_range(range(1..3))); +} + +#[test] +fn intersect() { + assert_eq!(range(1..2).intersect(range(2..3)), Some(range(2..2))); + assert_eq!(range(1..5).intersect(range(2..3)), Some(range(2..3))); + assert_eq!(range(1..2).intersect(range(3..4)), None); +} + +#[test] +fn cover() { + assert_eq!(range(1..2).cover(range(2..3)), range(1..3)); + assert_eq!(range(1..5).cover(range(2..3)), range(1..5)); + assert_eq!(range(1..2).cover(range(4..5)), range(1..5)); +} + +#[test] +fn cover_offset() { + assert_eq!(range(1..3).cover_offset(size(0)), range(0..3)); + assert_eq!(range(1..3).cover_offset(size(1)), range(1..3)); + assert_eq!(range(1..3).cover_offset(size(2)), range(1..3)); + assert_eq!(range(1..3).cover_offset(size(3)), range(1..3)); + assert_eq!(range(1..3).cover_offset(size(4)), range(1..4)); +} + +#[test] +#[rustfmt::skip] +fn contains_point() { + assert!( ! range(1..3).contains(size(0))); + assert!( range(1..3).contains(size(1))); + assert!( range(1..3).contains(size(2))); + assert!( ! range(1..3).contains(size(3))); + assert!( ! range(1..3).contains(size(4))); + + assert!( ! range(1..3).contains_inclusive(size(0))); + assert!( range(1..3).contains_inclusive(size(1))); + assert!( range(1..3).contains_inclusive(size(2))); + assert!( range(1..3).contains_inclusive(size(3))); + assert!( ! range(1..3).contains_inclusive(size(4))); +} diff --git a/crates/pglt_text_size/tests/serde.rs b/crates/pglt_text_size/tests/serde.rs new file mode 100644 index 00000000..e350b566 --- /dev/null +++ b/crates/pglt_text_size/tests/serde.rs @@ -0,0 +1,79 @@ +use {pglt_text_size::*, serde_test::*, std::ops}; + +fn size(x: u32) -> TextSize { + TextSize::from(x) +} + +fn range(x: ops::Range) -> TextRange { + TextRange::new(x.start.into(), x.end.into()) +} + +#[test] +fn size_serialization() { + assert_tokens(&size(00), &[Token::U32(00)]); + assert_tokens(&size(10), &[Token::U32(10)]); + assert_tokens(&size(20), &[Token::U32(20)]); + assert_tokens(&size(30), &[Token::U32(30)]); +} + +#[test] +fn range_serialization() { + assert_tokens( + &range(00..10), + &[ + Token::Tuple { len: 2 }, + Token::U32(00), + Token::U32(10), + Token::TupleEnd, + ], + ); + assert_tokens( + &range(10..20), + &[ + Token::Tuple { len: 2 }, + Token::U32(10), + Token::U32(20), + Token::TupleEnd, + ], + ); + assert_tokens( + &range(20..30), + &[ + Token::Tuple { len: 2 }, + Token::U32(20), + Token::U32(30), + Token::TupleEnd, + ], + ); + assert_tokens( + &range(30..40), + &[ + Token::Tuple { len: 2 }, + Token::U32(30), + Token::U32(40), + Token::TupleEnd, + ], + ); +} + +#[test] +fn invalid_range_deserialization() { + assert_tokens::( + &range(62..92), + &[ + Token::Tuple { len: 2 }, + Token::U32(62), + Token::U32(92), + Token::TupleEnd, + ], + ); + assert_de_tokens_error::( + &[ + Token::Tuple { len: 2 }, + Token::U32(92), + Token::U32(62), + Token::TupleEnd, + ], + "invalid range: 92..62", + ); +} diff --git a/crates/pglt_typecheck/Cargo.toml b/crates/pglt_typecheck/Cargo.toml index c078a922..19fc7b65 100644 --- a/crates/pglt_typecheck/Cargo.toml +++ b/crates/pglt_typecheck/Cargo.toml @@ -16,8 +16,8 @@ pglt_console.workspace = true pglt_diagnostics.workspace = true pglt_query_ext.workspace = true pglt_schema_cache.workspace = true +pglt_text_size.workspace = true sqlx.workspace = true -text-size.workspace = true tokio.workspace = true tree-sitter.workspace = true tree_sitter_sql.workspace = true diff --git a/crates/pglt_typecheck/src/diagnostics.rs b/crates/pglt_typecheck/src/diagnostics.rs index 495af5a6..741865cf 100644 --- a/crates/pglt_typecheck/src/diagnostics.rs +++ b/crates/pglt_typecheck/src/diagnostics.rs @@ -2,8 +2,8 @@ use std::io; use pglt_console::markup; use pglt_diagnostics::{Advices, Diagnostic, LogCategory, MessageAndDescription, Severity, Visit}; +use pglt_text_size::TextRange; use sqlx::postgres::{PgDatabaseError, PgSeverity}; -use text_size::TextRange; /// A specialized diagnostic for the typechecker. /// diff --git a/crates/pglt_typecheck/src/lib.rs b/crates/pglt_typecheck/src/lib.rs index 90b78777..341f21b1 100644 --- a/crates/pglt_typecheck/src/lib.rs +++ b/crates/pglt_typecheck/src/lib.rs @@ -2,11 +2,11 @@ mod diagnostics; pub use diagnostics::TypecheckDiagnostic; use diagnostics::create_type_error; +use pglt_text_size::TextRange; use sqlx::Executor; use sqlx::PgPool; use sqlx::postgres::PgDatabaseError; pub use sqlx::postgres::PgSeverity; -use text_size::TextRange; #[derive(Debug)] pub struct TypecheckParams<'a> { diff --git a/crates/pglt_workspace/Cargo.toml b/crates/pglt_workspace/Cargo.toml index 2f36d0d0..a31d0141 100644 --- a/crates/pglt_workspace/Cargo.toml +++ b/crates/pglt_workspace/Cargo.toml @@ -26,12 +26,12 @@ pglt_fs = { workspace = true, features = ["serde"] } pglt_query_ext = { workspace = true } pglt_schema_cache = { workspace = true } pglt_statement_splitter = { workspace = true } +pglt_text_size.workspace = true pglt_typecheck = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["raw_value"] } sqlx.workspace = true -text-size.workspace = true tokio = { workspace = true, features = ["rt", "rt-multi-thread"] } toml = { workspace = true } tracing = { workspace = true, features = ["attributes", "log"] } diff --git a/crates/pglt_workspace/src/workspace.rs b/crates/pglt_workspace/src/workspace.rs index 41139aac..b765ee74 100644 --- a/crates/pglt_workspace/src/workspace.rs +++ b/crates/pglt_workspace/src/workspace.rs @@ -4,8 +4,8 @@ pub use self::client::{TransportRequest, WorkspaceClient, WorkspaceTransport}; use pglt_analyse::RuleCategories; use pglt_configuration::{PartialConfiguration, RuleSelector}; use pglt_fs::PgLTPath; +use pglt_text_size::{TextRange, TextSize}; use serde::{Deserialize, Serialize}; -use text_size::{TextRange, TextSize}; use crate::WorkspaceError; diff --git a/crates/pglt_workspace/src/workspace/server/change.rs b/crates/pglt_workspace/src/workspace/server/change.rs index 174c75b9..5e3df205 100644 --- a/crates/pglt_workspace/src/workspace/server/change.rs +++ b/crates/pglt_workspace/src/workspace/server/change.rs @@ -1,5 +1,5 @@ +use pglt_text_size::{TextLen, TextRange, TextSize}; use std::ops::{Add, Sub}; -use text_size::{TextLen, TextRange, TextSize}; use crate::workspace::{ChangeFileParams, ChangeParams}; @@ -416,7 +416,7 @@ impl ChangeParams { mod tests { use super::*; use pglt_diagnostics::Diagnostic; - use text_size::TextRange; + use pglt_text_size::TextRange; use crate::workspace::{ChangeFileParams, ChangeParams}; diff --git a/crates/pglt_workspace/src/workspace/server/document.rs b/crates/pglt_workspace/src/workspace/server/document.rs index ec0c184d..cbb97a17 100644 --- a/crates/pglt_workspace/src/workspace/server/document.rs +++ b/crates/pglt_workspace/src/workspace/server/document.rs @@ -1,6 +1,6 @@ use pglt_diagnostics::{Diagnostic, DiagnosticExt, Severity, serde::Diagnostic as SDiagnostic}; use pglt_fs::PgLTPath; -use text_size::{TextRange, TextSize}; +use pglt_text_size::{TextRange, TextSize}; /// Global unique identifier for a statement #[derive(Debug, Hash, Eq, PartialEq, Clone)] diff --git a/lib/line_index/Cargo.toml b/lib/line_index/Cargo.toml deleted file mode 100644 index 55a438cb..00000000 --- a/lib/line_index/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "line_index" -version = "0.0.0" -edition = "2021" - -[dependencies] -text-size.workspace = true - -[lib] -doctest = false diff --git a/lib/line_index/src/lib.rs b/lib/line_index/src/lib.rs deleted file mode 100644 index 6b61bf11..00000000 --- a/lib/line_index/src/lib.rs +++ /dev/null @@ -1,217 +0,0 @@ -// The following code has been copied from rust-analyzer. - -//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` -//! representation. -use std::{collections::HashMap, iter}; - -use text_size::TextRange; -use text_size::TextSize; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct LineIndex { - /// Offset the the beginning of each line, zero-based - pub newlines: Vec, - /// List of non-ASCII characters on each line - pub(crate) utf16_lines: HashMap>, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct LineColUtf16 { - /// Zero-based - pub line: u32, - /// Zero-based - pub col: u32, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct LineCol { - /// Zero-based - pub line: u32, - /// Zero-based utf8 offset - pub col: u32, -} - -#[derive(Clone, Debug, Hash, PartialEq, Eq)] -pub(crate) struct Utf16Char { - /// Start offset of a character inside a line, zero-based - pub(crate) start: TextSize, - /// End offset of a character inside a line, zero-based - pub(crate) end: TextSize, -} - -impl Utf16Char { - /// Returns the length in 8-bit UTF-8 code units. - fn len(&self) -> TextSize { - self.end - self.start - } - - /// Returns the length in 16-bit UTF-16 code units. - fn len_utf16(&self) -> usize { - if self.len() == TextSize::from(4) { - 2 - } else { - 1 - } - } -} - -impl LineIndex { - pub fn new(text: &str) -> LineIndex { - let mut utf16_lines = HashMap::default(); - let mut utf16_chars = Vec::new(); - - let mut newlines = vec![0.into()]; - let mut curr_row = 0.into(); - let mut curr_col = 0.into(); - let mut line = 0; - for c in text.chars() { - let c_len = TextSize::of(c); - curr_row += c_len; - if c == '\n' { - newlines.push(curr_row); - - // Save any utf-16 characters seen in the previous line - if !utf16_chars.is_empty() { - utf16_lines.insert(line, utf16_chars); - utf16_chars = Vec::new(); - } - - // Prepare for processing the next line - curr_col = 0.into(); - line += 1; - continue; - } - - if !c.is_ascii() { - utf16_chars.push(Utf16Char { - start: curr_col, - end: curr_col + c_len, - }); - } - - curr_col += c_len; - } - - // Save any utf-16 characters seen in the last line - if !utf16_chars.is_empty() { - utf16_lines.insert(line, utf16_chars); - } - - LineIndex { - newlines, - utf16_lines, - } - } - - pub fn line_col(&self, offset: TextSize) -> LineCol { - let line = partition_point(&self.newlines, |&it| it <= offset) - 1; - let line_start_offset = self.newlines[line]; - let col = offset - line_start_offset; - LineCol { - line: line as u32, - col: col.into(), - } - } - - pub fn offset(&self, line_col: LineCol) -> Option { - Some(self.newlines.get(line_col.line as usize)? + TextSize::from(line_col.col)) - } - - pub fn to_utf16(&self, line_col: LineCol) -> Option { - let col = self.utf8_to_utf16_col(line_col.line, line_col.col.into()); - Some(LineColUtf16 { - line: line_col.line, - col: col as u32, - }) - } - - pub fn to_utf8(&self, line_col: LineColUtf16) -> Option { - let col = self.utf16_to_utf8_col(line_col.line, line_col.col); - Some(LineCol { - line: line_col.line, - col: col.into(), - }) - } - - pub fn lines(&self, range: TextRange) -> impl Iterator + '_ { - let lo = partition_point(&self.newlines, |&it| it < range.start()); - let hi = partition_point(&self.newlines, |&it| it <= range.end()); - let all = iter::once(range.start()) - .chain(self.newlines[lo..hi].iter().copied()) - .chain(iter::once(range.end())); - - all.clone() - .zip(all.skip(1)) - .map(|(lo, hi)| TextRange::new(lo, hi)) - .filter(|it| !it.is_empty()) - } - - fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize { - let mut res: usize = col.into(); - if let Some(utf16_chars) = self.utf16_lines.get(&line) { - for c in utf16_chars { - if c.end <= col { - res -= usize::from(c.len()) - c.len_utf16(); - } else { - // From here on, all utf16 characters come *after* the character we are mapping, - // so we don't need to take them into account - break; - } - } - } - res - } - - fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize { - if let Some(utf16_chars) = self.utf16_lines.get(&line) { - for c in utf16_chars { - if col > u32::from(c.start) { - col += u32::from(c.len()) - c.len_utf16() as u32; - } else { - // From here on, all utf16 characters come *after* the character we are mapping, - // so we don't need to take them into account - break; - } - } - } - - col.into() - } -} - -/// Returns `idx` such that: -/// -/// ```text -/// ∀ x in slice[..idx]: pred(x) -/// && ∀ x in slice[idx..]: !pred(x) -/// ``` -/// -/// https://github.com/rust-lang/rust/issues/73831 -fn partition_point(slice: &[T], mut pred: P) -> usize -where - P: FnMut(&T) -> bool, -{ - let mut left = 0; - let mut right = slice.len(); - - while left != right { - let mid = left + (right - left) / 2; - // SAFETY: - // When left < right, left <= mid < right. - // Therefore left always increases and right always decreases, - // and either of them is selected. - // In both cases left <= right is satisfied. - // Therefore if left < right in a step, - // left <= right is satisfied in the next step. - // Therefore as long as left != right, 0 <= left < right <= len is satisfied - // and if this case 0 <= mid < len is satisfied too. - let value = unsafe { slice.get_unchecked(mid) }; - if pred(value) { - left = mid + 1; - } else { - right = mid; - } - } - - left -}