Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit edc375d

Browse files
committed
Improve additionalUrls settings UI
1 parent 466a669 commit edc375d

File tree

7 files changed

+62
-33
lines changed

7 files changed

+62
-33
lines changed

package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,9 @@
482482
"description": "Path to a script relative to 'arduino.path', you can use a custom launch script to run Arduino by modifying this setting. Example: 'run-arduino.bat' for Windows, 'Contents/MacOS/run-arduino.sh' for Mac, 'bin/run-arduino.sh' for Linux. (Requires a restart after change)"
483483
},
484484
"arduino.additionalUrls": {
485-
"type": [
486-
"string",
487-
"array"
488-
],
489-
"description": "Additional URLs for 3-rd party packages. You can have multiple URLs in one string with comma(,) as separator, or have a string array."
485+
"type": "array",
486+
"items": { "type": "string" },
487+
"description": "Additional URLs for 3rd party packages."
490488
},
491489
"arduino.logLevel": {
492490
"type": "string",

src/arduino/arduinoContentProvider.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,19 @@ export class ArduinoContentProvider implements vscode.TextDocumentContentProvide
155155
}
156156
}
157157

158-
public openSettings(req, res) {
159-
vscode.commands.executeCommand("workbench.action.openGlobalSettings");
160-
return res.json({
161-
status: "OK",
162-
});
158+
public async openSettings(req, res) {
159+
if (!req.body.query) {
160+
return res.status(400).send("BAD Request! Missing { query } parameter!");
161+
} else {
162+
try {
163+
await vscode.commands.executeCommand("workbench.action.openGlobalSettings", { query: req.body.query });
164+
return res.json({
165+
status: "OK",
166+
});
167+
} catch (error) {
168+
return res.status(500).send(`Cannot open the setting with error message "${error}"`);
169+
}
170+
}
163171
}
164172

165173
public async getLibraries(req, res) {

src/arduino/boardManager.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -518,26 +518,12 @@ export class BoardManager {
518518
}
519519

520520
private getAdditionalUrls(): string[] {
521-
function formatUrls(urls): string[] {
522-
if (urls) {
523-
let _urls: string[];
524-
525-
if (!Array.isArray(urls) && typeof urls === "string") {
526-
_urls = (<string>urls).split(",");
527-
} else {
528-
_urls = <string[]>urls;
529-
}
530-
531-
return util.trim(_urls);
532-
}
533-
return [];
534-
}
535521
// For better compatibility, merge urls both in user settings and arduino IDE preferences.
536-
const settingsUrls = formatUrls(VscodeSettings.getInstance().additionalUrls);
522+
const settingsUrls = VscodeSettings.getInstance().additionalUrls;
537523
let preferencesUrls = [];
538524
const preferences = this._settings.preferences;
539525
if (preferences && preferences.has("boardsmanager.additional.urls")) {
540-
preferencesUrls = formatUrls(preferences.get("boardsmanager.additional.urls"));
526+
preferencesUrls = util.toStringArray(preferences.get("boardsmanager.additional.urls"));
541527
}
542528
return util.union(settingsUrls, preferencesUrls);
543529
}

src/arduino/vscodeSettings.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { toStringArray } from "../common/util";
56

67
const configKeys = {
78
ARDUINO_PATH: "arduino.path",
@@ -23,7 +24,7 @@ const configKeys = {
2324
export interface IVscodeSettings {
2425
arduinoPath: string;
2526
commandPath: string;
26-
additionalUrls: string | string[];
27+
additionalUrls: string[];
2728
logLevel: string;
2829
clearOutputOnBuild: boolean;
2930
allowPDEFiletype: boolean;
@@ -34,7 +35,7 @@ export interface IVscodeSettings {
3435
defaultBaudRate: number;
3536
useArduinoCli: boolean;
3637
disableIntelliSenseAutoGen: boolean;
37-
updateAdditionalUrls(urls: string | string[]): void;
38+
updateAdditionalUrls(urls: string[]): void;
3839
}
3940

4041
export class VscodeSettings implements IVscodeSettings {
@@ -57,8 +58,20 @@ export class VscodeSettings implements IVscodeSettings {
5758
return this.getConfigValue<string>(configKeys.ARDUINO_COMMAND_PATH);
5859
}
5960

60-
public get additionalUrls(): string | string[] {
61-
return this.getConfigValue<string | string[]>(configKeys.ADDITIONAL_URLS);
61+
public get additionalUrls(): string[] {
62+
const value = this.getConfigValue<string | string[]>(configKeys.ADDITIONAL_URLS);
63+
64+
// Even though the schema says value must be a string array, version
65+
// 0.4.9 and earlier also allowed a single comma delimeted string. We
66+
// continue to unofficially support that format to avoid breaking
67+
// existing settings, but we immediately write back the correctly
68+
// formatted version.
69+
const split = toStringArray(value);
70+
if (typeof value === 'string') {
71+
this.updateAdditionalUrls(split);
72+
}
73+
74+
return split;
6275
}
6376

6477
public get logLevel(): string {

src/common/util.ts

+22
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,25 @@ export function resolveMacArduinoAppPath(arduinoPath: string, useArduinoCli = fa
446446
return path.join(arduinoPath, "Arduino.app");
447447
}
448448
}
449+
450+
/**
451+
* If given an string, splits the string on commas. If given an array, returns
452+
* the array. All strings in the output are trimmed.
453+
* @param value String or string array to convert.
454+
* @returns Array of strings split from the input.
455+
*/
456+
export function toStringArray(value: string | string[]): string[] {
457+
if (value) {
458+
let result: string[];
459+
460+
if (typeof value === "string") {
461+
result = value.split(",");
462+
} else {
463+
result = <string[]>value;
464+
}
465+
466+
return trim(result);
467+
}
468+
469+
return [];
470+
}

src/views/app/actions/api.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ export function openLink(link) {
3737
}).then((response) => response.json());
3838
}
3939

40-
export function openSettings() {
41-
return postHTTP("/api/opensettings", {}).then((response) => response.json());
40+
export function openSettings(query) {
41+
return postHTTP("/api/opensettings", {
42+
query
43+
}).then((response) => response.json());
4244
}
4345

4446
export function getLibraries(update) {

src/views/app/components/BoardManager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class BoardManager extends React.Component<IBoardManagerProps, IBoardManagerStat
141141
<div className="arduinomanager-footer theme-bgcolor">
142142
<span>{totalCountTips}</span>
143143
<a className="help-link right-side" title="Configure Additional Boards Manager URLs"
144-
onClick={() => API.openSettings()}>Additional URLs</a>
144+
onClick={() => API.openSettings("arduino.additionalUrls")}>Additional URLs</a>
145145
</div>
146146

147147
</div>);

0 commit comments

Comments
 (0)