-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopen-command.ts
More file actions
144 lines (132 loc) · 6.83 KB
/
open-command.ts
File metadata and controls
144 lines (132 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Copyright 2026 Arm Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as vscode from 'vscode';
import { CommandsProvider } from '../../../vscode-api/commands-provider';
import { COutlineItem } from '../tree-structure/solution-outline-item';
import { PACKAGE_NAME, README_FILE_PATH } from '../../../manifest';
import path from 'path';
import { SolutionManager } from '../../../solutions/solution-manager';
import { IOpenFileExternal } from '../../../open-file-external-if';
import { contextDescriptorFromString } from '../../../solutions/descriptors/descriptors';
import { existsSync } from 'fs';
import { lineOf, readTextFile } from '../../../utils/fs-utils';
export class OpenCommand {
public static readonly openSolutionCommandId = `${PACKAGE_NAME}.openSolutionFile`;
public static readonly openProjectCommandId = `${PACKAGE_NAME}.openProjectFile`;
public static readonly openPrjConfCommandId = `${PACKAGE_NAME}.openPrjConfFile`;
public static readonly openLayerCommandId = `${PACKAGE_NAME}.openLayerFile`;
public static readonly openLinkerCommandId = `${PACKAGE_NAME}.openLinkerMapFile`;
public static readonly openDocCommandId = `${PACKAGE_NAME}.openDocFile`;
public static readonly openHelpCommandId = `${PACKAGE_NAME}.openHelp`;
public static readonly openZephyrTerminalCommandId = `${PACKAGE_NAME}.openZephyrTerminal`;
constructor(
private readonly solutionManager: SolutionManager,
private readonly commandsProvider: CommandsProvider,
private readonly openFileExternal: IOpenFileExternal,
) { }
public async activate(context: Pick<vscode.ExtensionContext, 'subscriptions'>) {
const keilPack = vscode.extensions.getExtension<void>('arm.keil-studio-pack');
let helpFilePath : string | undefined = undefined;
if (keilPack?.extensionPath) {
helpFilePath = path.join(keilPack.extensionPath, 'guide');
}
context.subscriptions.push(
this.commandsProvider.registerCommand(OpenCommand.openSolutionCommandId, () =>
(this.solutionManager.loadState.solutionPath) ? this.openFile(this.solutionManager.loadState.solutionPath) : undefined, this),
this.commandsProvider.registerCommand(OpenCommand.openProjectCommandId, (node: COutlineItem) => {
this.commandHandler(OpenCommand.openProjectCommandId, node);
}, this),
this.commandsProvider.registerCommand(OpenCommand.openPrjConfCommandId, (node: COutlineItem) => {
this.commandHandler(OpenCommand.openPrjConfCommandId, node);
}, this),
this.commandsProvider.registerCommand(OpenCommand.openLayerCommandId, (node: COutlineItem) => {
this.commandHandler(OpenCommand.openLayerCommandId, node);
}, this),
this.commandsProvider.registerCommand(OpenCommand.openLinkerCommandId, (node: COutlineItem) => {
this.commandHandler(OpenCommand.openLinkerCommandId, node);
}, this),
this.commandsProvider.registerCommand(OpenCommand.openDocCommandId, (node: COutlineItem) => {
this.commandHandler(OpenCommand.openDocCommandId, node);
}, this),
this.commandsProvider.registerCommand(OpenCommand.openZephyrTerminalCommandId, (node: COutlineItem) => {
this.openTerminal(node);
}, this),
this.commandsProvider.registerCommand(OpenCommand.openHelpCommandId, (section: string = 'index.html') => {
const nonSiblingPath = path.isAbsolute(section) || (path.normalize(section).startsWith('..'));
if (helpFilePath && existsSync(helpFilePath) && !nonSiblingPath) {
this.openFile(path.join(helpFilePath, section), true);
} else {
this.openFile(README_FILE_PATH, false);
}
}, this),
);
}
private async commandHandler(command: string, node: COutlineItem) {
const filePath = this.getFilePathForCommand(command, node);
if (filePath) {
if (command === OpenCommand.openProjectCommandId) {
this.openProjectFile(filePath);
return;
}
const openExternal = command === OpenCommand.openDocCommandId;
this.openFile(filePath, openExternal);
}
}
private async openProjectFile(filePath: string): Promise<void> {
const projectLine = lineOf(readTextFile(filePath), 'project:');
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(filePath));
const position = new vscode.Position(projectLine, 0);
await vscode.window.showTextDocument(document, {
selection: new vscode.Range(position, position),
preview: false,
});
}
private getFilePathForCommand(command: string, node: COutlineItem): string | undefined {
if (command === OpenCommand.openDocCommandId) {
return node.getAttribute('docPath');
} else if (command === OpenCommand.openPrjConfCommandId) {
return node.getAttribute('prjConfPath');
}
return node.getAttribute('resourcePath');
}
private openFile(path: string, openExternal?: boolean): void {
if (openExternal) {
this.openFileExternal.openFile(path);
} else if (path.toLowerCase().endsWith('.md')) {
this.commandsProvider.executeCommand('markdown.showPreview', vscode.Uri.file(path));
} else {
this.commandsProvider.executeCommand('vscode.open', vscode.Uri.file(path));
}
}
private openTerminal(node: COutlineItem): void {
const context = node.getAttribute('label');
if (context) {
const cbuildMap = this.solutionManager.getCsolution()?.cbuildYmlRoot;
if (cbuildMap) {
const cbuild = [...cbuildMap.keys()].find(
key => path.basename(key, '.cbuild.yml') === context) ?? undefined;
if (cbuild) {
const zephyrBuildDir = path.dirname(cbuild);
const terminal = vscode.window.createTerminal({
name: `Zephyr ${contextDescriptorFromString(context).projectName}`,
cwd: zephyrBuildDir,
});
terminal.show();
}
}
}
}
}