Skip to content

Commit ec847a4

Browse files
fix(completions): adjust range for later stmts (#615)
1 parent 0fc815a commit ec847a4

File tree

7 files changed

+130
-89
lines changed

7 files changed

+130
-89
lines changed

.github/workflows/pull_request.yml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,15 @@ jobs:
113113
sleep 1
114114
done
115115
116-
- uses: tree-sitter/setup-action@v2
116+
- uses: actions/cache@v5
117117
with:
118-
install-lib: false
118+
path: |
119+
~/.cargo/bin/tree-sitter
120+
~/.cargo/bin/sqlx
121+
key: ${{ runner.os }}-tree-sitter-${{ hashFiles('rust-toolchain.toml') }}
122+
123+
- name: Setup tree-sitter
124+
run: cargo install tree-sitter-cli
119125

120126
- name: Setup sqlx-cli
121127
run: cargo install sqlx-cli
@@ -160,6 +166,7 @@ jobs:
160166
uses: actions/checkout@v4
161167
with:
162168
submodules: true
169+
163170
- name: Free Disk Space
164171
uses: ./.github/actions/free-disk-space
165172
- name: Install toolchain
@@ -172,9 +179,14 @@ jobs:
172179
- name: Setup Postgres
173180
uses: ./.github/actions/setup-postgres
174181

175-
- uses: tree-sitter/setup-action@v2
182+
- uses: actions/cache@v5
176183
with:
177-
install-lib: false
184+
path: |
185+
~/.cargo/bin/tree-sitter
186+
key: ${{ runner.os }}-tree-sitter-${{ hashFiles('rust-toolchain.toml') }}
187+
188+
- name: Setup Postgres
189+
run: cargo install tree-sitter-cli
178190

179191
- name: Run tests
180192
run: cargo test --workspace
@@ -204,9 +216,13 @@ jobs:
204216
uses: moonrepo/setup-rust@v1
205217
with:
206218
cache-base: main
207-
- uses: tree-sitter/setup-action@v2
219+
- uses: actions/cache@v5
208220
with:
209-
install-lib: false
221+
path: |
222+
~/.cargo/bin/tree-sitter
223+
key: ${{ runner.os }}-tree-sitter-${{ hashFiles('rust-toolchain.toml') }}
224+
- name: setup tree-sitter
225+
run: cargo install tree-sitter-cli
210226
- name: Build main binary
211227
run: cargo build -p pgls_cli --release
212228
- name: Setup Bun
@@ -259,9 +275,13 @@ jobs:
259275
cache-base: main
260276
env:
261277
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
262-
- uses: tree-sitter/setup-action@v2
278+
- uses: actions/cache@v5
263279
with:
264-
install-lib: false
280+
path: |
281+
~/.cargo/bin/tree-sitter
282+
key: ${{ runner.os }}-tree-sitter-${{ hashFiles('rust-toolchain.toml') }}
283+
- name: setup tree-sitter
284+
run: cargo install tree-sitter-cli
265285
- name: Ensure RustFMT on nightly toolchain
266286
run: rustup component add rustfmt --toolchain nightly
267287
- name: echo toolchain

.github/workflows/release.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ jobs:
7272
sudo apt-get update
7373
sudo apt-get install -y musl-tools
7474
75-
- uses: tree-sitter/setup-action@v2
75+
- uses: actions/cache@v5
7676
with:
77-
install-lib: false
77+
path: |
78+
~/.cargo/bin/tree-sitter
79+
key: ${{ runner.os }}-tree-sitter-${{ hashFiles('rust-toolchain.toml') }}
80+
81+
- name: Setup tree-sitter
82+
run: cargo install tree-sitter-cli
7883

7984
- name: Setup Postgres
8085
uses: ./.github/actions/setup-postgres

.sqlx/query-b869d517301aaf69d382f092c09d5a53712d68afd273423f9310cd793586f532.json

Lines changed: 0 additions & 74 deletions
This file was deleted.

crates/pgls_lsp/src/handlers/completions.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ pub fn get_completions(
2020
let doc = session.document(&url)?;
2121
let encoding = adapters::negotiated_encoding(session.client_capabilities().unwrap());
2222

23-
let completion_result = match session.workspace.get_completions(GetCompletionsParams {
24-
path,
25-
position: get_cursor_position(session, &url, params.text_document_position.position)?,
26-
}) {
23+
let position = get_cursor_position(session, &url, params.text_document_position.position)?;
24+
25+
let completion_result = match session
26+
.workspace
27+
.get_completions(GetCompletionsParams { path, position })
28+
{
2729
Ok(result) => result,
2830
Err(e) => match e {
2931
WorkspaceError::DatabaseConnectionError(_) => {

crates/pgls_workspace/src/features/completions.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ pub struct CompletionsResult {
2121
pub(crate) items: Vec<CompletionItem>,
2222
}
2323

24+
impl CompletionsResult {
25+
pub fn with_offset(mut items: Vec<CompletionItem>, offset: TextSize) -> Self {
26+
for item in &mut items {
27+
if let Some(text_edit) = &mut item.completion_text {
28+
text_edit.range += offset;
29+
}
30+
}
31+
32+
Self { items }
33+
}
34+
}
35+
2436
impl IntoIterator for CompletionsResult {
2537
type Item = CompletionItem;
2638
type IntoIter = <Vec<CompletionItem> as IntoIterator>::IntoIter;

crates/pgls_workspace/src/workspace/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ impl Workspace for WorkspaceServer {
746746
text: id.content().to_string(),
747747
});
748748

749-
Ok(CompletionsResult { items })
749+
Ok(CompletionsResult::with_offset(items, range.start()))
750750
}
751751
}
752752
}

crates/pgls_workspace/src/workspace/server.tests.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,3 +793,79 @@ async fn test_search_path_configuration(test_db: PgPool) {
793793
);
794794
}
795795
}
796+
797+
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
798+
async fn test_multi_line_completions(test_db: PgPool) {
799+
let setup = r#"
800+
create schema auth;
801+
802+
create table auth.users (
803+
id serial primary key,
804+
email text not null
805+
);
806+
"#;
807+
808+
test_db.execute(setup).await.expect("setup sql failed");
809+
810+
let mut conf = PartialConfiguration::init();
811+
conf.merge_with(PartialConfiguration {
812+
db: Some(PartialDatabaseConfiguration {
813+
database: Some(
814+
test_db
815+
.connect_options()
816+
.get_database()
817+
.unwrap()
818+
.to_string(),
819+
),
820+
..Default::default()
821+
}),
822+
..Default::default()
823+
});
824+
825+
let workspace = get_test_workspace(Some(conf)).expect("Unable to create test workspace");
826+
827+
let path = PgLSPath::new("test.sql");
828+
829+
let content = r#"
830+
select * from auth.users;
831+
832+
select * from auth.|;
833+
834+
select * from auth.users;
835+
"#
836+
.trim();
837+
838+
let position = content
839+
.find('|')
840+
.map(|idx| pgls_text_size::TextSize::new(idx as u32))
841+
.expect("Unable to find cursor position in test content");
842+
843+
let sanitized_content = content.replace('|', "");
844+
845+
workspace
846+
.open_file(OpenFileParams {
847+
path: path.clone(),
848+
content: sanitized_content,
849+
version: 1,
850+
})
851+
.expect("Unable to open test file");
852+
853+
let completions = workspace
854+
.get_completions(crate::workspace::GetCompletionsParams {
855+
path: path.clone(),
856+
position,
857+
})
858+
.expect("Unable to request completions");
859+
860+
assert_eq!(
861+
completions.items.len(),
862+
1,
863+
"Expected one completion response"
864+
);
865+
866+
assert_eq!(
867+
completions.items[0].completion_text.as_ref().unwrap().range,
868+
TextRange::new(position, position),
869+
"Expected no syntax diagnostic"
870+
);
871+
}

0 commit comments

Comments
 (0)