Skip to content

Commit 7382c03

Browse files
authored
fix: improve problems service functions (#254)
1 parent 7e7b963 commit 7382c03

File tree

6 files changed

+143
-97
lines changed

6 files changed

+143
-97
lines changed

src/controller/problems.tsx

+19-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {
99
StatusBarService,
1010
ILayoutService,
1111
LayoutService,
12+
ProblemsService,
1213
} from 'mo/services';
1314
import { singleton, container } from 'tsyringe';
1415
import { builtInPanelProblems, builtInStatusProblems } from 'mo/model/problems';
1516
import { IMonacoService, MonacoService } from 'mo/monaco/monacoService';
1617
import { QuickTogglePanelAction } from 'mo/monaco/quickTogglePanelAction';
18+
import { ProblemsPaneView, ProblemsStatusBarView } from 'mo/workbench/problems';
19+
import { connect } from 'mo/react';
1720
export interface IProblemsController {
1821
onClick?: (e: React.MouseEvent, item: IStatusBarItem) => void;
1922
}
@@ -25,13 +28,16 @@ export class ProblemsController
2528
private readonly statusBarService: IStatusBarService;
2629
private readonly layoutService: ILayoutService;
2730
private readonly monacoService: IMonacoService;
31+
private readonly problemsService: ProblemsService;
2832

2933
constructor() {
3034
super();
3135
this.panelService = container.resolve(PanelService);
3236
this.statusBarService = container.resolve(StatusBarService);
3337
this.monacoService = container.resolve(MonacoService);
3438
this.layoutService = container.resolve(LayoutService);
39+
this.problemsService = container.resolve(ProblemsService);
40+
3541
this.init();
3642
}
3743

@@ -53,12 +59,19 @@ export class ProblemsController
5359
};
5460

5561
private init() {
56-
this.statusBarService.appendLeftItem(
57-
Object.assign(builtInStatusProblems(), {
58-
onClick: this.onClick,
59-
})
60-
);
61-
this.panelService.add(builtInPanelProblems());
62+
const statusProblems = builtInStatusProblems();
63+
statusProblems.render = (item) => <ProblemsStatusBarView {...item} />;
64+
statusProblems.onClick = this.onClick;
65+
66+
this.statusBarService.appendLeftItem(statusProblems);
67+
68+
// keep ProblemsPaneView updated to problems' state
69+
const ProblemsView = connect(this.problemsService, ProblemsPaneView);
70+
71+
const problemsPanel = builtInPanelProblems();
72+
problemsPanel.renderPane = () => <ProblemsView />;
73+
74+
this.panelService.add(problemsPanel);
6275
}
6376
}
6477

src/model/problems.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as React from 'react';
21
import { IStatusBarItem } from 'mo/model/workbench/statusBar';
32
import { IPanelItem } from 'mo/model/workbench/panel';
4-
import { ProblemsStatusBarView, ProblemsPaneView } from 'mo/workbench/problems';
53
import { localize } from 'mo/i18n/localize';
64

75
export enum MarkerSeverity {
@@ -45,7 +43,6 @@ export function builtInStatusProblems(): IStatusBarItem {
4543
infos: 0,
4644
},
4745
name: 'Problems',
48-
render: (item: IStatusBarItem) => <ProblemsStatusBarView {...item} />,
4946
};
5047
}
5148

@@ -54,7 +51,6 @@ export function builtInPanelProblems(): IPanelItem {
5451
id: PANEL_PROBLEMS,
5552
name: localize(PANEL_PROBLEMS, 'problems'),
5653
data: null,
57-
renderPane: (item) => <ProblemsPaneView {...item} />,
5854
};
5955
}
6056

src/react/component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export abstract class Component<S = any>
3434
* @param values update target state values
3535
*/
3636
public setState(
37-
values: S,
37+
values: Partial<S>,
3838
callback?: (prevState: S, nextState: S) => void
3939
) {
4040
const nextState = Object.assign(this.state, values);

src/services/problemsService.ts

+57-63
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,17 @@ import {
55
ProblemsModel,
66
MarkerSeverity,
77
builtInStatusProblems,
8-
builtInPanelProblems,
98
} from 'mo/model/problems';
10-
import { IPanelItem } from 'mo/model/workbench/panel';
119
import { IStatusBarItem } from 'mo/model/workbench/statusBar';
12-
import {
13-
PanelService,
14-
IPanelService,
15-
StatusBarService,
16-
IStatusBarService,
17-
} from 'mo/services';
10+
import { StatusBarService, IStatusBarService } from 'mo/services';
1811
import { Component } from 'mo/react';
1912
import { singleton, container } from 'tsyringe';
2013
import { searchById } from './helper';
2114
export interface IProblemsService extends Component<IProblems> {
22-
updateStatus<T>(item: IStatusBarItem<T>): void;
23-
updatePanel<T>(item: IStatusBarItem<T>): void;
24-
removeProblems(id: number): void;
15+
removeProblems(id: number | number[]): void;
2516
clearProblems(): void;
26-
addProblems(item: IProblemsItem): void;
27-
updateProblems<T>(item: IProblemsItem<T>): void;
28-
updateStatus(item: IStatusBarItem): void;
29-
updatePanel(item: IPanelItem): void;
17+
addProblems(item: IProblemsItem | IProblemsItem[]): void;
18+
updateProblems<T>(item: IProblemsItem<T> | IProblemsItem<T>[]): void;
3019
showHideProblems(): void;
3120
}
3221

@@ -35,12 +24,10 @@ export class ProblemsService
3524
extends Component<IProblems>
3625
implements IProblemsService {
3726
protected state: IProblems;
38-
private readonly panelService: IPanelService;
3927
private readonly statusBarService: IStatusBarService;
4028
constructor() {
4129
super();
4230
this.state = container.resolve(ProblemsModel);
43-
this.panelService = container.resolve(PanelService);
4431
this.statusBarService = container.resolve(StatusBarService);
4532
}
4633

@@ -50,85 +37,92 @@ export class ProblemsService
5037
show: !this.state.show,
5138
});
5239
}
53-
public addProblems<T>(item: IProblemsItem<T>): void {
40+
public addProblems<T>(item: IProblemsItem<T> | IProblemsItem<T>[]): void {
41+
const problems = Array.isArray(item) ? item : [item];
5442
const { data } = this.state;
55-
const index = data.findIndex(searchById(item.id));
56-
if (index > -1) {
57-
data.splice(index, 1, item);
58-
} else {
59-
data.push(item);
60-
}
43+
44+
problems.forEach((problem) => {
45+
const index = data.findIndex(searchById(problem.id));
46+
if (index > -1) {
47+
data.splice(index, 1, problem);
48+
} else {
49+
data.push(problem);
50+
}
51+
});
52+
6153
this.setState(
6254
{
63-
...this.state,
6455
data: [...data],
6556
},
6657
() => {
67-
this.update();
58+
this.updateStatusBar();
6859
}
6960
);
7061
}
71-
public updateProblems<T>(item: IProblemsItem<T>) {
62+
public updateProblems<T>(item: IProblemsItem<T> | IProblemsItem<T>[]) {
63+
const problems = Array.isArray(item) ? item : [item];
7264
const { data } = this.state;
73-
const index = data.findIndex(searchById(item.id));
74-
if (index > -1) {
75-
data.splice(index, 1, item);
76-
this.setState(
77-
{
78-
...this.state,
79-
data: [...data],
80-
},
81-
() => {
82-
this.update();
83-
}
84-
);
85-
} else {
86-
this.addProblems(item);
87-
}
65+
66+
problems.forEach((problem) => {
67+
const index = data.findIndex(searchById(problem.id));
68+
if (index > -1) {
69+
data.splice(index, 1, problem);
70+
}
71+
});
72+
73+
this.setState(
74+
{
75+
data: [...data],
76+
},
77+
() => {
78+
this.updateStatusBar();
79+
}
80+
);
8881
}
89-
public removeProblems(id: number): void {
82+
public removeProblems(id: number | number[]): void {
83+
const ids = Array.isArray(id) ? id : [id];
84+
9085
const { data = [] } = this.state;
91-
if (data.length > -1) {
92-
const index = data.findIndex(searchById(id));
86+
ids.forEach((problemId) => {
87+
const index = data.findIndex(searchById(problemId));
9388
if (index > -1) {
9489
data.splice(index, 1);
95-
this.setState(
96-
{
97-
...this.state,
98-
data: [...data],
99-
},
100-
() => {
101-
this.update();
102-
}
103-
);
10490
}
105-
}
91+
});
92+
93+
this.setState(
94+
{
95+
data: [...data],
96+
},
97+
() => {
98+
this.updateStatusBar();
99+
}
100+
);
106101
}
102+
107103
public clearProblems(): void {
108104
this.setState({
109105
...this.state,
110106
data: [],
111107
});
112108
this.updateStatus(builtInStatusProblems());
113-
this.updatePanel(builtInPanelProblems());
114109
}
115-
public update<T>(): void {
110+
111+
private updateStatusBar<T>(): void {
116112
const { data = [] } = this.state;
117113
const markersData = this.getProblemsMarkers(data);
118114
this.updateStatus(
119115
Object.assign(builtInStatusProblems(), {
120116
data: markersData,
121117
})
122118
);
123-
this.updatePanel(Object.assign(builtInPanelProblems(), { data }));
124119
}
125-
public updateStatus<T>(item: IStatusBarItem<T>): void {
120+
121+
private updateStatus<T>(item: IStatusBarItem<T>): void {
126122
this.statusBarService.updateItem(item);
127123
}
128-
public updatePanel<T>(item: IPanelItem<T>): void {
129-
this.panelService.update(item);
130-
}
131-
public getProblemsMarkers = (
124+
125+
private getProblemsMarkers = (
132126
data: IProblemsItem[]
133127
): { warnings: number; errors: number; infos: number } => {
134128
let warnings = 0;

src/workbench/problems/paneView/index.tsx

+27-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { getBEMElement, prefixClaName } from 'mo/common/className';
33
import TreeView from 'mo/components/tree';
44
import { localize } from 'mo/i18n/localize';
5+
import { Scrollable } from 'mo/components';
56

67
const defaultClassName = prefixClaName('problems');
78
const treeClassName = getBEMElement(defaultClassName, 'treeview');
@@ -23,32 +24,35 @@ function ProblemsPaneView(props: any) {
2324
);
2425

2526
return (
26-
<div className={defaultClassName}>
27-
<TreeView
28-
className={treeClassName}
29-
data={data}
30-
renderTitle={({ children, name, value }, _, isLeaf) => {
31-
return !isLeaf ? (
32-
<span className={treeNodeClassName}>
33-
{value.code}
34-
<span className={treeNodeBadgeClassName}>
35-
{children?.length}
36-
</span>
37-
</span>
38-
) : (
39-
<span className={treeLeafClassName}>
40-
{value.message}
41-
<span className={treeLeafSubInfoClassName}>
27+
<Scrollable>
28+
<div className={defaultClassName}>
29+
<TreeView
30+
className={treeClassName}
31+
data={data}
32+
renderTitle={({ children, name, value }, _, isLeaf) => {
33+
return !isLeaf ? (
34+
<span className={treeNodeClassName}>
4235
{value.code}
36+
<span className={treeNodeBadgeClassName}>
37+
{children?.length}
38+
</span>
4339
</span>
44-
<span className={treeLeafSubInfoClassName}>
45-
[{value.startLineNumber}, {value.startColumn}]
40+
) : (
41+
<span className={treeLeafClassName}>
42+
{value.message}
43+
<span className={treeLeafSubInfoClassName}>
44+
{value.code}
45+
</span>
46+
<span className={treeLeafSubInfoClassName}>
47+
[{value.startLineNumber},{' '}
48+
{value.startColumn}]
49+
</span>
4650
</span>
47-
</span>
48-
);
49-
}}
50-
/>
51-
</div>
51+
);
52+
}}
53+
/>
54+
</div>
55+
</Scrollable>
5256
);
5357
}
5458

stories/extensions/test/testPane.tsx

+39
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ export default class TestPane extends React.Component {
9898
molecule.panel.appendOutput('Number: ' + Math.random() * 10 + '\n');
9999
};
100100

101+
const updateProblem = () => {
102+
const problems = molecule.problems.getState().data;
103+
molecule.problems.addProblems({
104+
id: (problems[problems.length - 1]?.id || 0) + 10,
105+
name: 'text.tsx',
106+
value: {
107+
code: 'text.tsx',
108+
message: '文件夹',
109+
startLineNumber: 0,
110+
startColumn: 1,
111+
endLineNumber: 0,
112+
endColumn: 1,
113+
status: 1,
114+
},
115+
children: [
116+
{
117+
id: (problems[problems.length - 1]?.id || 0) + 1,
118+
name: '0-1',
119+
value: {
120+
code: 'endLineNumber',
121+
message: '语法错误',
122+
startLineNumber: 0,
123+
startColumn: 1,
124+
endLineNumber: 0,
125+
endColumn: 1,
126+
status: 2,
127+
},
128+
children: [],
129+
},
130+
],
131+
});
132+
};
133+
134+
const clearProblems = () => {
135+
molecule.problems.clearProblems();
136+
};
137+
101138
const newEditor = function () {
102139
const key = (Math.random() * 10 + 1).toFixed(2);
103140
const tabData: IEditorTab = {
@@ -270,6 +307,8 @@ PARTITIONED BY (ds string) lifecycle 1000;
270307
<Button onClick={addPanel}>Add Panel</Button>
271308
<Button onClick={showHidePanel}>Show/Hide Panel</Button>
272309
<Button onClick={updateOutput}>Update Output</Button>
310+
<Button onClick={updateProblem}>Update Problem</Button>
311+
<Button onClick={clearProblems}>Clear Problem</Button>
273312
</div>
274313
<div style={{ margin: '50px 20px' }}>
275314
<h2>Notification:</h2>

0 commit comments

Comments
 (0)