Skip to content

Commit b97a486

Browse files
committed
Add basic infrastructure for using Rust in the vscode extension
1 parent c48f145 commit b97a486

File tree

11 files changed

+141
-29
lines changed

11 files changed

+141
-29
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ jobs:
166166
if: matrix.os == 'ubuntu-latest' && needs.changes.outputs.typescript == 'true'
167167
run: sudo apt-get install -y xvfb
168168

169+
- name: Install wasm-pack
170+
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
171+
169172
- run: npm ci
170173
working-directory: ./editors/code
171174
if: needs.changes.outputs.typescript == 'true'

Cargo.lock

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
2-
members = ["xtask/", "lib/*", "crates/*"]
2+
members = ["xtask/", "lib/*", "crates/*", "editors/code/wasm"]
33
exclude = ["crates/proc-macro-srv/proc-macro-test/imp"]
4+
45
resolver = "2"
56

67
[workspace.package]

crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
359359
&'a self,
360360
ctor: &'a rustc_pattern_analysis::constructor::Constructor<Self>,
361361
ty: &'a Self::Ty,
362-
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a> {
362+
) -> impl ExactSizeIterator<Item = Self::Ty> + Captures<'a> {
363363
let single = |ty| smallvec![ty];
364364
let tys: SmallVec<[_; 2]> = match ctor {
365365
Struct | Variant(_) | UnionField => match ty.kind(Interner) {

editors/code/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
},
1313
plugins: ["@typescript-eslint"],
1414
rules: {
15-
camelcase: ["error"],
15+
camelcase: ["off"], // This lint causes more trouble than good when mixing Rust and TS
1616
eqeqeq: ["error", "always", { null: "ignore" }],
1717
curly: ["error", "multi-line"],
1818
"no-console": ["error", { allow: ["warn", "error"] }],

editors/code/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.vscode-test
33
out
4+
wasm/pkg

editors/code/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@
3232
"enabledApiProposals": [],
3333
"scripts": {
3434
"vscode:prepublish": "npm run build-base -- --minify",
35-
"package": "vsce package -o rust-analyzer.vsix",
35+
"package": "vsce package -o rust-analyzer.vsix && npm run patch-vsix",
36+
"patch-vsix": "bash -c 'mkdir -p extension/out && cp wasm/pkg/* extension/out/ && zip -ur rust-analyzer.vsix extension/out/* && rm -r extension'",
3637
"build-base": "esbuild ./src/main.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node --target=node16",
38+
"build-wasm": "cd wasm && wasm-pack build -t nodejs",
3739
"build": "npm run build-base -- --sourcemap",
3840
"watch": "npm run build-base -- --sourcemap --watch",
3941
"format": "prettier --write .",
4042
"format:check": "prettier --check .",
4143
"lint": "eslint -c .eslintrc.js --ext ts ./src ./tests",
4244
"lint:fix": "npm run lint -- --fix",
43-
"typecheck": "tsc",
45+
"typecheck": "npm run build-wasm && tsc",
4446
"pretest": "npm run typecheck && npm run build",
4547
"test": "node ./out/tests/runTests.js"
4648
},

editors/code/src/main.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as diagnostics from "./diagnostics";
77
import { activateTaskProvider } from "./tasks";
88
import { setContextValue } from "./util";
99
import type { JsonProject } from "./rust_project";
10+
import { check_conflicting_extensions } from "../wasm/pkg/vscode_ra_wasm";
1011

1112
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
1213

@@ -22,10 +23,15 @@ export async function deactivate() {
2223
await setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined);
2324
}
2425

26+
// @ts-ignore
27+
globalThis.vscode_window = vscode.window;
28+
// @ts-ignore
29+
globalThis.vscode_extensions = vscode.extensions;
30+
2531
export async function activate(
2632
context: vscode.ExtensionContext,
2733
): Promise<RustAnalyzerExtensionApi> {
28-
checkConflictingExtensions();
34+
check_conflicting_extensions();
2935

3036
const ctx = new Ctx(context, createCommands(), fetchWorkspace());
3137
// VS Code doesn't show a notification when an extension fails to activate
@@ -191,26 +197,3 @@ function createCommands(): Record<string, CommandFactory> {
191197
revealDependency: { enabled: commands.revealDependency },
192198
};
193199
}
194-
195-
function checkConflictingExtensions() {
196-
if (vscode.extensions.getExtension("rust-lang.rust")) {
197-
vscode.window
198-
.showWarningMessage(
199-
`You have both the rust-analyzer (rust-lang.rust-analyzer) and Rust (rust-lang.rust) ` +
200-
"plugins enabled. These are known to conflict and cause various functions of " +
201-
"both plugins to not work correctly. You should disable one of them.",
202-
"Got it",
203-
)
204-
.then(() => {}, console.error);
205-
}
206-
207-
if (vscode.extensions.getExtension("panicbit.cargo")) {
208-
vscode.window
209-
.showWarningMessage(
210-
`You have both the rust-analyzer (rust-lang.rust-analyzer) and Cargo (panicbit.cargo) plugins enabled, ` +
211-
'you can disable it or set {"cargo.automaticCheck": false} in settings.json to avoid invoking cargo twice',
212-
"Got it",
213-
)
214-
.then(() => {}, console.error);
215-
}
216-
}

editors/code/wasm/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "vscode_ra_wasm"
3+
version = "0.1.0"
4+
rust-version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
authors.workspace = true
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[dependencies]
13+
wasm-bindgen = "0.2.74"

editors/code/wasm/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
pub mod vscode;
4+
5+
#[wasm_bindgen]
6+
pub fn check_conflicting_extensions() {
7+
if vscode::extensions::getExtension("rust-lang.rust").is_truthy() {
8+
vscode::window::showWarningMessage(
9+
"You have both the rust-analyzer (rust-lang.rust-analyzer) and Rust (rust-lang.rust) \
10+
plugins enabled. These are known to conflict and cause various functions of \
11+
both plugins to not work correctly. You should disable one of them.",
12+
"Got it",
13+
);
14+
}
15+
16+
if vscode::extensions::getExtension("panicbit.cargo").is_truthy() {
17+
vscode::window::showWarningMessage(
18+
"You have both the rust-analyzer (rust-lang.rust-analyzer) and Cargo (panicbit.cargo) plugins enabled, \
19+
you can disable it or set {'cargo.automaticCheck': false} in settings.json to avoid invoking cargo twice",
20+
"Got it",
21+
);
22+
}
23+
}

editors/code/wasm/src/vscode.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod window {
2+
use wasm_bindgen::prelude::*;
3+
4+
#[wasm_bindgen]
5+
extern "C" {
6+
#[wasm_bindgen(js_namespace = vscode_window)]
7+
pub fn showWarningMessage(message: &str, button: &str);
8+
}
9+
}
10+
11+
pub mod extensions {
12+
use wasm_bindgen::prelude::*;
13+
14+
#[wasm_bindgen]
15+
extern "C" {
16+
#[wasm_bindgen(js_namespace = vscode_extensions)]
17+
pub fn getExtension(s: &str) -> JsValue;
18+
}
19+
}

0 commit comments

Comments
 (0)