Skip to content

Commit 9f1158b

Browse files
committed
完善项目管理功能:添加从 workspace.json 文件获取当前项目的功能,并在激活时标记文件;更新 workspace.json 文件以保存当前项目;增强文件装饰提供者以支持清除标记的文件。
1 parent bd6bf3a commit 9f1158b

File tree

5 files changed

+100
-40
lines changed

5 files changed

+100
-40
lines changed

src/extension.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { initAPI } from './api';
1515
import { openWorkspaceProjectsWebview } from './webviews/project';
1616
import { initProjectTree } from './project/tree';
1717
import { DecorationProvider } from './project/fileDecorationProvider';
18+
import { getCurrentProjectInWorkspace } from './webviews/project';
1819

1920
let _context: vscode.ExtensionContext;
2021

@@ -50,6 +51,12 @@ export async function activate(context: vscode.ExtensionContext) {
5051
context.workspaceState.update('isRTThreadWorksapce', isRTThreadWorksapce);
5152

5253
new DecorationProvider(context);
54+
55+
// get current project from workspace.json file
56+
let currentProject = getCurrentProjectInWorkspace();
57+
if (currentProject) {
58+
DecorationProvider.getInstance().markFile(vscode.Uri.file(currentProject));
59+
}
5360
}
5461
}
5562
else {

src/project/cmd.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vscode from 'vscode';
44

55
import { getWorkspaceFolder } from '../api';
66
import { executeCommand } from '../terminal';
7+
import { readWorkspaceJson, writeWorkspaceJson } from '../webviews/project';
78

89
let _currentProject: string = '';
910

@@ -44,6 +45,13 @@ export function setCurrentProject(arg: any) {
4445

4546
let cmd = 'scons -C ' + arg.fn + ' --target=vsc_workspace';
4647
executeCommand(cmd);
48+
49+
// update workspace.json file
50+
let workspaceJson = readWorkspaceJson();
51+
if (workspaceJson) {
52+
workspaceJson.currentProject = arg.fn;
53+
writeWorkspaceJson(workspaceJson);
54+
}
4755
}
4856

4957
return;

src/project/fileDecorationProvider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ export class DecorationProvider implements FileDecorationProvider {
5858
this._onDidChangeFileDecorations.fire(uri);
5959
}
6060
}
61+
62+
public async unmarkAllFiles() {
63+
this.markedFiles.clear();
64+
}
6165
}

src/vue/projects/App.vue

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
可以在感兴趣的BSP/工程项上✔,然后保存配置,将会在侧边栏中显示对应列表。<br>
66
<hr>
77

8-
<el-button @click="reloadBSPProjects" hidden>加载列表</el-button>
9-
<el-button @click="collapseAll">折叠全部列表</el-button>
10-
11-
<el-button type="primary" @click="saveBSPProjects">保存列表配置</el-button>
8+
<div style="text-align: right; margin-bottom: 10px;">
9+
<el-button @click="reloadBSPProjects" style="display: none;">加载列表</el-button>
10+
<el-button @click="collapseAll">折叠全部列表</el-button>
11+
<el-button type="primary" @click="saveBSPProjects">保存列表配置</el-button>
12+
</div>
1213

1314
<el-table ref="tableRef" v-loading="loading" :data="tableData" style="width: 100%" row-key="id"
1415
:expand-row-keys="expandedRowKeys">
@@ -20,7 +21,7 @@
2021
</template>
2122

2223
<script setup lang="ts">
23-
import { onMounted, ref } from 'vue';
24+
import { onMounted, ref, nextTick } from 'vue';
2425
import { imgUrl } from '../assets/img';
2526
import { sendCommand } from '../api/vscode';
2627
@@ -42,9 +43,11 @@ const reloadBSPProjects = () => {
4243
4344
const saveBSPProjects = () => {
4445
let args:string[] = [];
45-
const selectedRows = tableRef.value.getSelectionRows();
46-
if (selectedRows.length > 0) {
47-
args = selectedRows.map(row => row.path);
46+
if (tableRef.value) {
47+
const selectedRows = (tableRef.value as any).getSelectionRows();
48+
if (selectedRows.length > 0) {
49+
args = selectedRows.map(row => row.path);
50+
}
4851
}
4952
5053
sendCommand('saveBSPProjects', [args]);
@@ -59,14 +62,20 @@ onMounted(() => {
5962
6063
switch (message.command) {
6164
case 'updateProjects':
62-
// console.log(message);
6365
tableData.value = message.data.dirs;
6466
let stars:string[] = message.data.stars;
65-
tableData.value.forEach((item, index) => {
66-
if (stars.includes(item.path)) {
67-
tableRef.value?.toggleRowSelection({ id: item.id }, true)
68-
}
67+
68+
// 使用 nextTick 确保 DOM 更新完成后再执行选择操作
69+
nextTick(() => {
70+
tableData.value.forEach((item, index) => {
71+
if (stars.includes(item.path)) {
72+
if (tableRef.value) {
73+
(tableRef.value as any).toggleRowSelection(item, true);
74+
}
75+
}
76+
});
6977
});
78+
7079
loading.value = false; // 停止加载动画
7180
break;
7281

src/webviews/project.ts

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,40 @@ export function openWorkspaceProjectsWebview(context: vscode.ExtensionContext) {
122122
});
123123
});
124124
panel.webview.onDidReceiveMessage(message => {
125+
125126
switch (message.command) {
126127
case 'searchBSPProjects':
127-
let workspaceJson = path.join(getWorkspaceFolder() + '/' + '.vscode', 'workspace.json');
128-
if (fs.existsSync(workspaceJson)) {
129-
let j = JSON.parse(fs.readFileSync(workspaceJson, 'utf8'));
130-
if (j.hasOwnProperty("bsps")) {
131-
let bsps = j["bsps"];
132-
if (bsps.hasOwnProperty("folder")) {
133-
let bspFolder = getWorkspaceFolder() + '/' + bsps.folder;
134-
135-
findRtconfigDirectories(bspFolder).then((dirs) => {
136-
let stars:string[] = [];
137-
if (bsps.hasOwnProperty("stars")) {
138-
stars = bsps.stars;
139-
}
140-
141-
panel.webview.postMessage({command: 'updateProjects', data: {dirs: dirs, stars: stars}});
142-
});
128+
{
129+
let workspaceJson = readWorkspaceJson();
130+
if (workspaceJson) {
131+
if (workspaceJson.hasOwnProperty("bsps")) {
132+
let bsps = workspaceJson["bsps"];
133+
if (bsps.hasOwnProperty("folder")) {
134+
let bspFolder = getWorkspaceFolder() + '/' + bsps.folder;
135+
136+
findRtconfigDirectories(bspFolder).then((dirs) => {
137+
let stars:string[] = [];
138+
if (bsps.hasOwnProperty("stars")) {
139+
stars = bsps.stars;
140+
}
141+
142+
panel.webview.postMessage({command: 'updateProjects', data: {dirs: dirs, stars: stars}});
143+
});
144+
}
143145
}
144146
}
145147
}
146-
147148
break;
148149

149150
case 'saveBSPProjects':
150-
let stars = message.args[0];
151-
// save the stars to the workspace.json file
152-
let workspaceFile = path.join(getWorkspaceFolder() + '/' + '.vscode', 'workspace.json');
153-
if (fs.existsSync(workspaceFile)) {
154-
let j = JSON.parse(fs.readFileSync(workspaceFile, 'utf8'));
155-
if (j.hasOwnProperty("bsps")) {
156-
let bsps = j["bsps"];
157-
bsps.stars = stars;
158-
fs.writeFileSync(workspaceFile, JSON.stringify(j, null, 4), 'utf8');
159-
}
151+
{
152+
let stars = message.args[0];
153+
// save the stars to the workspace.json file
154+
let workspaceJson = readWorkspaceJson();
155+
if (workspaceJson) {
156+
workspaceJson.bsps.stars = stars;
157+
writeWorkspaceJson(workspaceJson);
158+
}
160159
}
161160
break;
162161
}},
@@ -168,3 +167,36 @@ export function openWorkspaceProjectsWebview(context: vscode.ExtensionContext) {
168167

169168
return workspaceViewPanel;
170169
}
170+
171+
// read workspace.json file
172+
export function readWorkspaceJson() {
173+
let workspaceJson = path.join(getWorkspaceFolder() + '/' + '.vscode', 'workspace.json');
174+
if (fs.existsSync(workspaceJson)) {
175+
return JSON.parse(fs.readFileSync(workspaceJson, 'utf8'));
176+
}
177+
return null;
178+
}
179+
180+
// write workspace.json file
181+
export function writeWorkspaceJson(data: any) {
182+
let workspaceJson = path.join(getWorkspaceFolder() + '/' + '.vscode', 'workspace.json');
183+
fs.writeFileSync(workspaceJson, JSON.stringify(data, null, 4), 'utf8');
184+
}
185+
186+
// set current project in workspace.json file
187+
export function setCurrentProjectInWorkspace(project: string) {
188+
let workspaceJson = readWorkspaceJson();
189+
if (workspaceJson) {
190+
workspaceJson.currentProject = project;
191+
writeWorkspaceJson(workspaceJson);
192+
}
193+
}
194+
195+
// get current project from workspace.json file
196+
export function getCurrentProjectInWorkspace() {
197+
let workspaceJson = readWorkspaceJson();
198+
if (workspaceJson) {
199+
return workspaceJson.currentProject;
200+
}
201+
return null;
202+
}

0 commit comments

Comments
 (0)