diff options
Diffstat (limited to 'editors/code/src/config.ts')
-rw-r--r-- | editors/code/src/config.ts | 123 |
1 files changed, 14 insertions, 109 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index b45b14bef..54b905303 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts | |||
@@ -1,9 +1,5 @@ | |||
1 | import * as os from "os"; | ||
2 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
3 | import { ArtifactSource } from "./installation/interfaces"; | 2 | import { log } from "./util"; |
4 | import { log, vscodeReloadWindow } from "./util"; | ||
5 | |||
6 | const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; | ||
7 | 3 | ||
8 | export interface InlayHintOptions { | 4 | export interface InlayHintOptions { |
9 | typeHints: boolean; | 5 | typeHints: boolean; |
@@ -25,10 +21,7 @@ export interface CargoFeatures { | |||
25 | loadOutDirsFromCheck: boolean; | 21 | loadOutDirsFromCheck: boolean; |
26 | } | 22 | } |
27 | 23 | ||
28 | export const enum UpdatesChannel { | 24 | export type UpdatesChannel = "stable" | "nightly"; |
29 | Stable = "stable", | ||
30 | Nightly = "nightly" | ||
31 | } | ||
32 | 25 | ||
33 | export const NIGHTLY_TAG = "nightly"; | 26 | export const NIGHTLY_TAG = "nightly"; |
34 | export class Config { | 27 | export class Config { |
@@ -41,26 +34,21 @@ export class Config { | |||
41 | "cargo-watch", | 34 | "cargo-watch", |
42 | "highlighting.semanticTokens", | 35 | "highlighting.semanticTokens", |
43 | "inlayHints", | 36 | "inlayHints", |
37 | "updates.channel", | ||
44 | ] | 38 | ] |
45 | .map(opt => `${this.rootSection}.${opt}`); | 39 | .map(opt => `${this.rootSection}.${opt}`); |
46 | 40 | ||
47 | readonly packageJsonVersion = vscode | 41 | readonly packageJsonVersion: string = vscode |
48 | .extensions | 42 | .extensions |
49 | .getExtension(this.extensionId)! | 43 | .getExtension(this.extensionId)! |
50 | .packageJSON | 44 | .packageJSON |
51 | .version as string; // n.n.YYYYMMDD[-nightly] | 45 | .version; |
52 | |||
53 | /** | ||
54 | * Either `nightly` or `YYYY-MM-DD` (i.e. `stable` release) | ||
55 | */ | ||
56 | readonly extensionReleaseTag: string = (() => { | ||
57 | if (this.packageJsonVersion.endsWith(NIGHTLY_TAG)) return NIGHTLY_TAG; | ||
58 | |||
59 | const realVersionRegexp = /^\d+\.\d+\.(\d{4})(\d{2})(\d{2})/; | ||
60 | const [, yyyy, mm, dd] = this.packageJsonVersion.match(realVersionRegexp)!; | ||
61 | 46 | ||
62 | return `${yyyy}-${mm}-${dd}`; | 47 | readonly releaseTag: string = vscode |
63 | })(); | 48 | .extensions |
49 | .getExtension(this.extensionId)! | ||
50 | .packageJSON | ||
51 | .releaseTag; | ||
64 | 52 | ||
65 | private cfg!: vscode.WorkspaceConfiguration; | 53 | private cfg!: vscode.WorkspaceConfiguration; |
66 | 54 | ||
@@ -94,100 +82,17 @@ export class Config { | |||
94 | ); | 82 | ); |
95 | 83 | ||
96 | if (userResponse === "Reload now") { | 84 | if (userResponse === "Reload now") { |
97 | await vscodeReloadWindow(); | 85 | await vscode.commands.executeCommand("workbench.action.reloadWindow"); |
98 | } | ||
99 | } | ||
100 | |||
101 | private static replaceTildeWithHomeDir(path: string) { | ||
102 | if (path.startsWith("~/")) { | ||
103 | return os.homedir() + path.slice("~".length); | ||
104 | } | ||
105 | return path; | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Name of the binary artifact for `rust-analyzer` that is published for | ||
110 | * `platform` on GitHub releases. (It is also stored under the same name when | ||
111 | * downloaded by the extension). | ||
112 | */ | ||
113 | get prebuiltServerFileName(): null | string { | ||
114 | // See possible `arch` values here: | ||
115 | // https://nodejs.org/api/process.html#process_process_arch | ||
116 | |||
117 | switch (process.platform) { | ||
118 | |||
119 | case "linux": { | ||
120 | switch (process.arch) { | ||
121 | case "arm": | ||
122 | case "arm64": return null; | ||
123 | |||
124 | default: return "rust-analyzer-linux"; | ||
125 | } | ||
126 | } | ||
127 | |||
128 | case "darwin": return "rust-analyzer-mac"; | ||
129 | case "win32": return "rust-analyzer-windows.exe"; | ||
130 | |||
131 | // Users on these platforms yet need to manually build from sources | ||
132 | case "aix": | ||
133 | case "android": | ||
134 | case "freebsd": | ||
135 | case "openbsd": | ||
136 | case "sunos": | ||
137 | case "cygwin": | ||
138 | case "netbsd": return null; | ||
139 | // The list of platforms is exhaustive (see `NodeJS.Platform` type definition) | ||
140 | } | ||
141 | } | ||
142 | |||
143 | get installedExtensionUpdateChannel(): UpdatesChannel { | ||
144 | return this.extensionReleaseTag === NIGHTLY_TAG | ||
145 | ? UpdatesChannel.Nightly | ||
146 | : UpdatesChannel.Stable; | ||
147 | } | ||
148 | |||
149 | get serverSource(): null | ArtifactSource { | ||
150 | const serverPath = RA_LSP_DEBUG ?? this.serverPath; | ||
151 | |||
152 | if (serverPath) { | ||
153 | return { | ||
154 | type: ArtifactSource.Type.ExplicitPath, | ||
155 | path: Config.replaceTildeWithHomeDir(serverPath) | ||
156 | }; | ||
157 | } | 86 | } |
158 | |||
159 | const prebuiltBinaryName = this.prebuiltServerFileName; | ||
160 | |||
161 | if (!prebuiltBinaryName) return null; | ||
162 | |||
163 | return this.createGithubReleaseSource( | ||
164 | prebuiltBinaryName, | ||
165 | this.extensionReleaseTag | ||
166 | ); | ||
167 | } | 87 | } |
168 | 88 | ||
169 | private createGithubReleaseSource(file: string, tag: string): ArtifactSource.GithubRelease { | 89 | get globalStoragePath(): string { return this.ctx.globalStoragePath; } |
170 | return { | ||
171 | type: ArtifactSource.Type.GithubRelease, | ||
172 | file, | ||
173 | tag, | ||
174 | dir: this.ctx.globalStoragePath, | ||
175 | repo: { | ||
176 | name: "rust-analyzer", | ||
177 | owner: "rust-analyzer", | ||
178 | } | ||
179 | }; | ||
180 | } | ||
181 | |||
182 | get nightlyVsixSource(): ArtifactSource.GithubRelease { | ||
183 | return this.createGithubReleaseSource("rust-analyzer.vsix", NIGHTLY_TAG); | ||
184 | } | ||
185 | 90 | ||
186 | // We don't do runtime config validation here for simplicity. More on stackoverflow: | 91 | // We don't do runtime config validation here for simplicity. More on stackoverflow: |
187 | // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension | 92 | // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension |
188 | 93 | ||
189 | private get serverPath() { return this.cfg.get("serverPath") as null | string; } | 94 | get serverPath() { return this.cfg.get("serverPath") as null | string; } |
190 | get updatesChannel() { return this.cfg.get("updates.channel") as UpdatesChannel; } | 95 | get channel() { return this.cfg.get<"stable" | "nightly">("updates.channel")!; } |
191 | get askBeforeDownload() { return this.cfg.get("updates.askBeforeDownload") as boolean; } | 96 | get askBeforeDownload() { return this.cfg.get("updates.askBeforeDownload") as boolean; } |
192 | get highlightingSemanticTokens() { return this.cfg.get("highlighting.semanticTokens") as boolean; } | 97 | get highlightingSemanticTokens() { return this.cfg.get("highlighting.semanticTokens") as boolean; } |
193 | get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; } | 98 | get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; } |