Skip to content

Commit

Permalink
Some code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Apr 5, 2024
1 parent cefe28c commit 77e11b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 44 deletions.
13 changes: 8 additions & 5 deletions src/commands/launchTargetPath.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default class LaunchTargetPathCommand extends CommandWithResult<string> {
}

private async readProjectNameFromCMakeLists(
filename: string): Promise<string | null> {
filename: string
): Promise<string | null> {
// Read the file
const fileContent = readFileSync(filename, "utf-8");

Expand All @@ -26,7 +27,7 @@ export default class LaunchTargetPathCommand extends CommandWithResult<string> {
// Extract the project name from the matched result
if (match && match[1]) {
const projectName = match[1].trim();

if (matchBg && matchPoll) {
// For examples with both background and poll, let user pick which to run
const quickPickItems = ["Threadsafe Background", "Poll"];
Expand All @@ -36,7 +37,7 @@ export default class LaunchTargetPathCommand extends CommandWithResult<string> {
if (backgroundOrPoll === undefined) {
return projectName;
}

switch (backgroundOrPoll) {
case quickPickItems[0]:
return projectName + "_background";
Expand Down Expand Up @@ -69,7 +70,9 @@ export default class LaunchTargetPathCommand extends CommandWithResult<string> {
return "";
}

return join(fsPathFolder, "build", projectName + ".elf")
.replaceAll("\\", "/");
return join(fsPathFolder, "build", projectName + ".elf").replaceAll(
"\\",
"/"
);
}
}
62 changes: 28 additions & 34 deletions src/commands/openSdkDocumentation.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable max-len */
import { CommandWithArgs } from "./command.mjs";
import Logger from "../logger.mjs";
import { type QuickPickItem, ViewColumn, window, Uri, type WebviewPanel } from "vscode";
import {
type QuickPickItem,
ViewColumn,
window,
Uri,
type WebviewPanel,
} from "vscode";
import { readFileSync } from "fs";

export enum DocumentationId {
Expand Down Expand Up @@ -38,7 +44,7 @@ export default class OpenSdkDocumentationCommand extends CommandWithArgs {
private _logger: Logger = new Logger("OpenSdkDocumentationCommand");

public static readonly id = "openSdkDocumentation";
private _panel: WebviewPanel|undefined = undefined;
private _panel: WebviewPanel | undefined = undefined;

constructor(private readonly _extensionUri: Uri) {
super(OpenSdkDocumentationCommand.id);
Expand Down Expand Up @@ -73,7 +79,7 @@ export default class OpenSdkDocumentationCommand extends CommandWithArgs {

// show webview
this._logger.info("Opening SDK documentation in browser...");
if (this._panel === undefined){
if (this._panel === undefined) {
Logger.log("New panel");
this._panel = window.createWebviewPanel(
"pico-sdk-documentation",
Expand All @@ -100,11 +106,9 @@ export default class OpenSdkDocumentationCommand extends CommandWithArgs {
panel.reveal();

// dispose when hidden
panel.onDidDispose(
event => {
this._panel = undefined;
}, this
);
panel.onDidDispose(() => {
this._panel = undefined;
}, this);
}

private _getHtmlForWebview(
Expand Down Expand Up @@ -133,49 +137,39 @@ export default class OpenSdkDocumentationCommand extends CommandWithArgs {
if (space === undefined) {
space = "";
}
if (file.match('#') || file.match('https')) {
if (file.match('#')) {
file = `#${file.split('#')[1]}`;
if (file.match("#") || file.match("https")) {
if (file.match("#")) {
file = `#${file.split("#")[1]}`;
}
const ret = `<a ${space}href="${file}"`;

return ret;
}
const command = Uri.parse(`command:raspberry-pi-pico.openSdkDocumentation?${
encodeURIComponent(JSON.stringify([undefined, file]))
}`).toString();
const command = Uri.parse(
`command:raspberry-pi-pico.openSdkDocumentation?${encodeURIComponent(
JSON.stringify([undefined, file])
)}`
).toString();
const ret = `<a ${space}href="${command}"`;

return ret;
})
.replace(regexsrc, function (match, file: string) {
const ret = `src="${
panel.webview
.asWebviewUri(
Uri.joinPath(extensionUri, "web", "docs", file)
)
.toString()}"`;
const ret = `src="${panel.webview
.asWebviewUri(Uri.joinPath(extensionUri, "web", "docs", file))
.toString()}"`;

return ret;
})
.replace(regexcss, function (match, file: string) {
const ret = `<link href="${
panel.webview
.asWebviewUri(
Uri.joinPath(extensionUri, "web", "docs", file)
)
.toString()}" rel="stylesheet"`;
const ret = `<link href="${panel.webview
.asWebviewUri(Uri.joinPath(extensionUri, "web", "docs", file))
.toString()}" rel="stylesheet"`;

return ret;
})
.replace(
'<div class="navigation-toggle">',
'<div hidden>'
)
.replace(
'<div id="main-nav">',
'<div id="main-nav" hidden>'
)
.replace('<div class="navigation-toggle">', "<div hidden>")
.replace('<div id="main-nav">', '<div id="main-nav" hidden>')
);
}
}
10 changes: 5 additions & 5 deletions src/settings.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ const LAST_PROJECT_ROOT_STATE_KEY = "lastProjectRoot";
export default class Settings {
private static instance?: Settings;
private config: WorkspaceConfiguration;
public context: Memento;
public workspaceState: Memento;
public globalState: GlobalStateType;
private pkg: PackageJSON;

private constructor(
context: Memento,
workspaceState: Memento,
globalState: GlobalStateType,
packageJSON: PackageJSON
) {
this.context = context;
this.workspaceState = workspaceState;
this.globalState = globalState;
this.pkg = packageJSON;

this.config = workspace.getConfiguration(packageJSON.name);
}

public static createInstance(
context: Memento,
workspaceState: Memento,
globalState: GlobalStateType,
packageJSON: PackageJSON
): Settings {
Settings.instance = new Settings(context, globalState, packageJSON);
Settings.instance = new Settings(workspaceState, globalState, packageJSON);

return Settings.instance;
}
Expand Down

0 comments on commit 77e11b6

Please sign in to comment.