Skip to content

Commit 3736294

Browse files
to textsize in lsp crate
1 parent a09643f commit 3736294

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

crates/pg_lsp_new/src/handlers/completions.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::session::Session;
22
use anyhow::Result;
3-
use pg_workspace_new::workspace::CompletionParams;
3+
use pg_workspace_new::workspace;
44
use tower_lsp::lsp_types::{self, CompletionItem, CompletionItemLabelDetails};
55

66
#[tracing::instrument(level = "debug", skip_all)]
@@ -10,13 +10,28 @@ pub fn get_completions(
1010
) -> Result<lsp_types::CompletionResponse> {
1111
let url = params.text_document_position.text_document.uri;
1212
let path = session.file_path(&url)?;
13-
let column = params.text_document_position.position.character;
14-
let line = params.text_document_position.position.line;
1513

16-
let completion_result =
17-
session
18-
.workspace
19-
.get_completions(CompletionParams { path, line, column })?;
14+
let client_capabilities = session
15+
.client_capabilities()
16+
.expect("Client capabilities not established for current session.");
17+
18+
let line_index = session
19+
.document(&url)
20+
.map(|doc| doc.line_index)
21+
.map_err(|_| anyhow::anyhow!("Document not found."))?;
22+
23+
let offset = pg_lsp_converters::from_proto::offset(
24+
&line_index,
25+
params.text_document_position.position,
26+
pg_lsp_converters::negotiated_encoding(client_capabilities),
27+
)?;
28+
29+
let completion_result = session
30+
.workspace
31+
.get_completions(workspace::CompletionParams {
32+
path,
33+
position: offset,
34+
})?;
2035

2136
let items: Vec<CompletionItem> = completion_result
2237
.into_iter()

crates/pg_lsp_new/src/session.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use std::sync::Arc;
2222
use std::sync::RwLock;
2323
use tokio::sync::Notify;
2424
use tokio::sync::OnceCell;
25-
use tower_lsp::lsp_types;
2625
use tower_lsp::lsp_types::Url;
26+
use tower_lsp::lsp_types::{self, ClientCapabilities};
2727
use tower_lsp::lsp_types::{MessageType, Registration};
2828
use tower_lsp::lsp_types::{Unregistration, WorkspaceFolder};
2929
use tracing::{error, info};
@@ -385,6 +385,13 @@ impl Session {
385385
self.initialize_params.get()?.client_information.as_ref()
386386
}
387387

388+
/// Returns a reference to the client capabilities for this session
389+
pub(crate) fn client_capabilities(&self) -> Option<&ClientCapabilities> {
390+
self.initialize_params
391+
.get()
392+
.map(|params| &params.client_capabilities)
393+
}
394+
388395
/// This function attempts to read the `pglsp.toml` configuration file from
389396
/// the root URI and update the workspace settings accordingly
390397
#[tracing::instrument(level = "trace", skip(self))]

crates/pg_workspace_new/src/workspace.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub struct CompletionParams {
4444
/// The File for which a completion is requested.
4545
pub path: PgLspPath,
4646
/// The Cursor position in the file for which a completion is requested.
47-
pub column: u32,
48-
pub line: u32,
47+
pub position: TextSize,
4948
}
5049

5150
#[derive(Debug, serde::Serialize, serde::Deserialize)]

crates/pg_workspace_new/src/workspace/server.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,13 @@ impl Workspace for WorkspaceServer {
353353
.get(&params.path)
354354
.ok_or(WorkspaceError::not_found())?;
355355

356-
let offset = doc.line_and_col_to_offset(params.line, params.column);
357-
358356
tracing::info!(
359357
"Found the document. Looking for statement in file {:?} at position: {:?}",
360358
&params.path,
361-
&offset
359+
&params.position
362360
);
363361

364-
let statement = match doc.statement_at_offset(&offset) {
362+
let statement = match doc.statement_at_offset(&params.position) {
365363
Some(s) => s,
366364
None => return Ok(pg_completions::CompletionResult::default()),
367365
};
@@ -372,7 +370,7 @@ impl Workspace for WorkspaceServer {
372370
let stmt_range = doc
373371
.statement_range(statement_ref)
374372
.expect("Range of statement should be defined.");
375-
let position = offset - stmt_range.start();
373+
let position = params.position - stmt_range.start();
376374

377375
let tree = self.tree_sitter.fetch(statement_ref);
378376
let text = statement.text;

0 commit comments

Comments
 (0)