Skip to content

Commit

Permalink
Replace Location by Position
Browse files Browse the repository at this point in the history
  • Loading branch information
BinderDavid committed Jan 6, 2025
1 parent 86e53a9 commit bac3ccb
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 71 deletions.
1 change: 1 addition & 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 @@ -32,6 +32,7 @@ categories = ["compilers"]
miette = { version = "7" }
thiserror = { version = "1" }
# lsp server
lsp-types = { version = "0.97" }
tower-lsp = { git = "https://github.com/tower-lsp/tower-lsp", rev = "19f5a87810ff4b643d2bc394e438450bd9c74365", default-features = false, features = [
"runtime-agnostic",
] }
Expand Down
1 change: 1 addition & 0 deletions lang/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ categories.workspace = true
async-trait = "0.1"
# adressing symbols
url = { workspace = true }
lsp-types = { workspace = true }
# index of source code intervals
rust-lapper = "1"
# text rope
Expand Down
7 changes: 4 additions & 3 deletions lang/driver/src/codespan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use miette_util::codespan::{ByteIndex, ColumnIndex, LineIndex, LineOffset, Location, Span};
use lsp_types::Position;
use miette_util::codespan::{ByteIndex, LineIndex, LineOffset, Span};

/// An enum representing an error that happened while looking up a file or a piece of content in that file.
#[derive(Debug)]
Expand Down Expand Up @@ -60,7 +61,7 @@ impl File {
}
}

Check warning on line 62 in lang/driver/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/codespan.rs#L62

Added line #L62 was not covered by tests

pub fn location(&self, byte_index: ByteIndex) -> Result<Location, Error> {
pub fn location(&self, byte_index: ByteIndex) -> Result<Position, Error> {
let line_index = self.line_index(byte_index);
let line_start_index = self.line_start(line_index).map_err(|_| Error::IndexTooLarge {
given: byte_index.to_usize(),
Expand All @@ -79,7 +80,7 @@ impl File {
}
})?;

Check warning on line 81 in lang/driver/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/codespan.rs#L81

Added line #L81 was not covered by tests

Ok(Location { line: line_index, column: ColumnIndex(line_src.chars().count() as u32) })
Ok(Position { line: line_index.0, character: line_src.chars().count() as u32 })
}

Check warning on line 84 in lang/driver/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/codespan.rs#L83-L84

Added lines #L83 - L84 were not covered by tests

pub fn source(&self) -> &String {
Expand Down
15 changes: 8 additions & 7 deletions lang/driver/src/spans.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
use miette_util::codespan::{ByteIndex, Location, Span};
use lsp_types::{Position, Range};
use miette_util::codespan::{ByteIndex, LineIndex, Span};
use url::Url;

use crate::database::Database;

use super::info::{Info, Item};

impl Database {
pub fn location_to_index(&self, uri: &Url, location: Location) -> Option<ByteIndex> {
pub fn location_to_index(&self, uri: &Url, location: Position) -> Option<ByteIndex> {

Check warning on line 10 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L10

Added line #L10 was not covered by tests
let file = self.files.get_even_if_stale(uri).unwrap();
let line_span = file.line_span(location.line).ok()?;
let index: usize = line_span.start().to_usize() + location.column.to_usize();
let line_span = file.line_span(LineIndex(location.line)).ok()?;
let index: usize = line_span.start().to_usize() + LineIndex(location.character).to_usize();
Some(ByteIndex(index as u32))

Check warning on line 14 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L12-L14

Added lines #L12 - L14 were not covered by tests
}

pub fn index_to_location(&self, uri: &Url, idx: ByteIndex) -> Option<Location> {
pub fn index_to_location(&self, uri: &Url, idx: ByteIndex) -> Option<Position> {

Check warning on line 17 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L17

Added line #L17 was not covered by tests
let file = self.files.get_even_if_stale(uri).unwrap();
file.location(idx).ok()
}

pub fn span_to_locations(&self, uri: &Url, span: Span) -> Option<(Location, Location)> {
pub fn span_to_locations(&self, uri: &Url, span: Span) -> Option<Range> {

Check warning on line 22 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L22

Added line #L22 was not covered by tests
let start = self.index_to_location(uri, span.start())?;
let end = self.index_to_location(uri, span.end())?;
Some((start, end))
Some(Range { start, end })

Check warning on line 25 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L25

Added line #L25 was not covered by tests
}

pub async fn hoverinfo_at_index(&mut self, uri: &Url, idx: ByteIndex) -> Option<Info> {
Expand Down
9 changes: 3 additions & 6 deletions lang/lsp/src/codeactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub async fn code_action(
.await;

let mut db = server.database.write().await;
let span_start = db.location_to_index(&text_document.uri.from_lsp(), range.start.from_lsp());
let span_end = db.location_to_index(&text_document.uri.from_lsp(), range.end.from_lsp());
let span_start = db.location_to_index(&text_document.uri.from_lsp(), range.start);
let span_end = db.location_to_index(&text_document.uri.from_lsp(), range.end);
let span = span_start
.and_then(|start| span_end.map(|end| miette_util::codespan::Span::new(start, end)));

Check warning on line 29 in lang/lsp/src/codeactions.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/codeactions.rs#L26-L29

Added lines #L26 - L29 were not covered by tests
let item = if let Some(span) = span {
Expand All @@ -42,10 +42,7 @@ pub async fn code_action(
let edits = edits
.into_iter()
.map(|edit| TextEdit {
range: db
.span_to_locations(&text_document.uri.from_lsp(), edit.span)
.unwrap()
.to_lsp(),
range: db.span_to_locations(&text_document.uri.from_lsp(), edit.span).unwrap(),

Check warning on line 45 in lang/lsp/src/codeactions.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/codeactions.rs#L45

Added line #L45 was not covered by tests
new_text: edit.text,
})
.collect();
Expand Down
1 change: 0 additions & 1 deletion lang/lsp/src/conversion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use miette::Severity;
use tower_lsp::lsp_types::DiagnosticSeverity;

mod spans;
mod uri_to_url;

pub trait FromLsp {
Expand Down
31 changes: 0 additions & 31 deletions lang/lsp/src/conversion/spans.rs

This file was deleted.

6 changes: 2 additions & 4 deletions lang/lsp/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ impl Diagnostics for Database {
// the default range is used, which corresponds to the beginning of the
// file.
let span = get_span(&error);
let range = span
.and_then(|x| self.span_to_locations(uri, x.from_miette()))
.map(ToLsp::to_lsp)
.unwrap_or_default();
let range =
span.and_then(|x| self.span_to_locations(uri, x.from_miette())).unwrap_or_default();

Check warning on line 32 in lang/lsp/src/diagnostics.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/diagnostics.rs#L31-L32

Added lines #L31 - L32 were not covered by tests

// Compute the message.
let message = error.to_string();
Expand Down
4 changes: 2 additions & 2 deletions lang/lsp/src/gotodefinition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn goto_definition(

let pos = pos_params.position;
let mut db = server.database.write().await;
let info = db.location_to_index(&text_document.uri.from_lsp(), pos.from_lsp());
let info = db.location_to_index(&text_document.uri.from_lsp(), pos);

Check warning on line 27 in lang/lsp/src/gotodefinition.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/gotodefinition.rs#L27

Added line #L27 was not covered by tests
let info = match info {
Some(idx) => db.hoverinfo_at_index(&text_document.uri.from_lsp(), idx).await,
None => None,
Expand All @@ -40,7 +40,7 @@ fn info_to_jump(db: &Database, info: Info) -> Option<GotoDefinitionResponse> {
}

fn span_to_location(span: &Span, uri: &Uri, db: &Database) -> Option<Location> {
let range = db.span_to_locations(&uri.from_lsp(), *span).map(ToLsp::to_lsp)?;
let range = db.span_to_locations(&uri.from_lsp(), *span)?;

Check warning on line 43 in lang/lsp/src/gotodefinition.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/gotodefinition.rs#L43

Added line #L43 was not covered by tests
Some(Location { uri: uri.clone(), range })
}

Expand Down
4 changes: 2 additions & 2 deletions lang/lsp/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn hover(server: &Server, params: HoverParams) -> jsonrpc::Result<Opti

let pos = pos_params.position;
let mut db = server.database.write().await;
let info = db.location_to_index(&text_document.uri.from_lsp(), pos.from_lsp());
let info = db.location_to_index(&text_document.uri.from_lsp(), pos);

Check warning on line 23 in lang/lsp/src/hover.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/hover.rs#L23

Added line #L23 was not covered by tests

let info = match info {
Some(idx) => db.hoverinfo_at_index(&text_document.uri.from_lsp(), idx).await,
Expand All @@ -32,7 +32,7 @@ pub async fn hover(server: &Server, params: HoverParams) -> jsonrpc::Result<Opti
}

fn info_to_hover(db: &Database, uri: &Uri, info: Info) -> Hover {
let range = db.span_to_locations(&uri.from_lsp(), info.span).map(ToLsp::to_lsp);
let range = db.span_to_locations(&uri.from_lsp(), info.span);

Check warning on line 35 in lang/lsp/src/hover.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/hover.rs#L35

Added line #L35 was not covered by tests
let contents = info.content.to_hover_content();
Hover { contents, range }
}
Expand Down
15 changes: 0 additions & 15 deletions lang/miette_util/src/codespan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,3 @@ impl fmt::Display for ColumnIndex {
self.0.fmt(f)
}

Check warning on line 238 in lang/miette_util/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/miette_util/src/codespan.rs#L236-L238

Added lines #L236 - L238 were not covered by tests
}
/// A location in a source file.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Location {
/// The line index in the source file.
pub line: LineIndex,
/// The column index in the source file.
pub column: ColumnIndex,
}

impl Location {
/// Construct a new location from a line index and a column index.
pub fn new(line: impl Into<LineIndex>, column: impl Into<ColumnIndex>) -> Location {
Location { line: line.into(), column: column.into() }
}
}

0 comments on commit bac3ccb

Please sign in to comment.