Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve problems service functions #254

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/controller/problems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import {
StatusBarService,
ILayoutService,
LayoutService,
ProblemsService,
} from 'mo/services';
import { singleton, container } from 'tsyringe';
import { builtInPanelProblems, builtInStatusProblems } from 'mo/model/problems';
import { IMonacoService, MonacoService } from 'mo/monaco/monacoService';
import { QuickTogglePanelAction } from 'mo/monaco/quickTogglePanelAction';
import { ProblemsPaneView, ProblemsStatusBarView } from 'mo/workbench/problems';
import { connect } from 'mo/react';
export interface IProblemsController {
onClick?: (e: React.MouseEvent, item: IStatusBarItem) => void;
}
Expand All @@ -25,13 +28,16 @@ export class ProblemsController
private readonly statusBarService: IStatusBarService;
private readonly layoutService: ILayoutService;
private readonly monacoService: IMonacoService;
private readonly problemsService: ProblemsService;

constructor() {
super();
this.panelService = container.resolve(PanelService);
this.statusBarService = container.resolve(StatusBarService);
this.monacoService = container.resolve(MonacoService);
this.layoutService = container.resolve(LayoutService);
this.problemsService = container.resolve(ProblemsService);

this.init();
}

Expand All @@ -53,12 +59,19 @@ export class ProblemsController
};

private init() {
this.statusBarService.appendLeftItem(
Object.assign(builtInStatusProblems(), {
onClick: this.onClick,
})
);
this.panelService.add(builtInPanelProblems());
const statusProblems = builtInStatusProblems();
statusProblems.render = (item) => <ProblemsStatusBarView {...item} />;
statusProblems.onClick = this.onClick;

this.statusBarService.appendLeftItem(statusProblems);

// keep ProblemsPaneView updated to problems' state
const ProblemsView = connect(this.problemsService, ProblemsPaneView);

const problemsPanel = builtInPanelProblems();
problemsPanel.renderPane = () => <ProblemsView />;

this.panelService.add(problemsPanel);
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/model/problems.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as React from 'react';
import { IStatusBarItem } from 'mo/model/workbench/statusBar';
import { IPanelItem } from 'mo/model/workbench/panel';
import { ProblemsStatusBarView, ProblemsPaneView } from 'mo/workbench/problems';
import { localize } from 'mo/i18n/localize';

export enum MarkerSeverity {
Expand Down Expand Up @@ -45,7 +43,6 @@ export function builtInStatusProblems(): IStatusBarItem {
infos: 0,
},
name: 'Problems',
render: (item: IStatusBarItem) => <ProblemsStatusBarView {...item} />,
};
}

Expand All @@ -54,7 +51,6 @@ export function builtInPanelProblems(): IPanelItem {
id: PANEL_PROBLEMS,
name: localize(PANEL_PROBLEMS, 'problems'),
data: null,
renderPane: (item) => <ProblemsPaneView {...item} />,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/react/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export abstract class Component<S = any>
* @param values update target state values
*/
public setState(
values: S,
values: Partial<S>,
callback?: (prevState: S, nextState: S) => void
) {
const nextState = Object.assign(this.state, values);
Expand Down
120 changes: 57 additions & 63 deletions src/services/problemsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,17 @@ import {
ProblemsModel,
MarkerSeverity,
builtInStatusProblems,
builtInPanelProblems,
} from 'mo/model/problems';
import { IPanelItem } from 'mo/model/workbench/panel';
import { IStatusBarItem } from 'mo/model/workbench/statusBar';
import {
PanelService,
IPanelService,
StatusBarService,
IStatusBarService,
} from 'mo/services';
import { StatusBarService, IStatusBarService } from 'mo/services';
import { Component } from 'mo/react';
import { singleton, container } from 'tsyringe';
import { searchById } from './helper';
export interface IProblemsService extends Component<IProblems> {
updateStatus<T>(item: IStatusBarItem<T>): void;
updatePanel<T>(item: IStatusBarItem<T>): void;
removeProblems(id: number): void;
removeProblems(id: number | number[]): void;
clearProblems(): void;
addProblems(item: IProblemsItem): void;
updateProblems<T>(item: IProblemsItem<T>): void;
updateStatus(item: IStatusBarItem): void;
updatePanel(item: IPanelItem): void;
addProblems(item: IProblemsItem | IProblemsItem[]): void;
updateProblems<T>(item: IProblemsItem<T> | IProblemsItem<T>[]): void;
showHideProblems(): void;
}

Expand All @@ -35,12 +24,10 @@ export class ProblemsService
extends Component<IProblems>
implements IProblemsService {
protected state: IProblems;
private readonly panelService: IPanelService;
private readonly statusBarService: IStatusBarService;
constructor() {
super();
this.state = container.resolve(ProblemsModel);
this.panelService = container.resolve(PanelService);
this.statusBarService = container.resolve(StatusBarService);
}

Expand All @@ -50,85 +37,92 @@ export class ProblemsService
show: !this.state.show,
});
}
public addProblems<T>(item: IProblemsItem<T>): void {
public addProblems<T>(item: IProblemsItem<T> | IProblemsItem<T>[]): void {
const problems = Array.isArray(item) ? item : [item];
const { data } = this.state;
const index = data.findIndex(searchById(item.id));
if (index > -1) {
data.splice(index, 1, item);
} else {
data.push(item);
}

problems.forEach((problem) => {
const index = data.findIndex(searchById(problem.id));
if (index > -1) {
data.splice(index, 1, problem);
} else {
data.push(problem);
}
});

this.setState(
{
...this.state,
data: [...data],
},
() => {
this.update();
this.updateStatusBar();
}
);
}
public updateProblems<T>(item: IProblemsItem<T>) {
public updateProblems<T>(item: IProblemsItem<T> | IProblemsItem<T>[]) {
const problems = Array.isArray(item) ? item : [item];
const { data } = this.state;
const index = data.findIndex(searchById(item.id));
if (index > -1) {
data.splice(index, 1, item);
this.setState(
{
...this.state,
data: [...data],
},
() => {
this.update();
}
);
} else {
this.addProblems(item);
}

problems.forEach((problem) => {
const index = data.findIndex(searchById(problem.id));
if (index > -1) {
data.splice(index, 1, problem);
}
});

this.setState(
{
data: [...data],
},
() => {
this.updateStatusBar();
}
);
}
public removeProblems(id: number): void {
public removeProblems(id: number | number[]): void {
const ids = Array.isArray(id) ? id : [id];

const { data = [] } = this.state;
if (data.length > -1) {
const index = data.findIndex(searchById(id));
ids.forEach((problemId) => {
const index = data.findIndex(searchById(problemId));
if (index > -1) {
data.splice(index, 1);
this.setState(
{
...this.state,
data: [...data],
},
() => {
this.update();
}
);
}
}
});

this.setState(
{
data: [...data],
},
() => {
this.updateStatusBar();
}
);
}

public clearProblems(): void {
this.setState({
...this.state,
data: [],
});
this.updateStatus(builtInStatusProblems());
this.updatePanel(builtInPanelProblems());
}
public update<T>(): void {

private updateStatusBar<T>(): void {
const { data = [] } = this.state;
const markersData = this.getProblemsMarkers(data);
this.updateStatus(
Object.assign(builtInStatusProblems(), {
data: markersData,
})
);
this.updatePanel(Object.assign(builtInPanelProblems(), { data }));
}
public updateStatus<T>(item: IStatusBarItem<T>): void {

private updateStatus<T>(item: IStatusBarItem<T>): void {
this.statusBarService.updateItem(item);
}
public updatePanel<T>(item: IPanelItem<T>): void {
this.panelService.update(item);
}
public getProblemsMarkers = (

private getProblemsMarkers = (
data: IProblemsItem[]
): { warnings: number; errors: number; infos: number } => {
let warnings = 0;
Expand Down
50 changes: 27 additions & 23 deletions src/workbench/problems/paneView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { getBEMElement, prefixClaName } from 'mo/common/className';
import TreeView from 'mo/components/tree';
import { localize } from 'mo/i18n/localize';
import { Scrollable } from 'mo/components';

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

return (
<div className={defaultClassName}>
<TreeView
className={treeClassName}
data={data}
renderTitle={({ children, name, value }, _, isLeaf) => {
return !isLeaf ? (
<span className={treeNodeClassName}>
{value.code}
<span className={treeNodeBadgeClassName}>
{children?.length}
</span>
</span>
) : (
<span className={treeLeafClassName}>
{value.message}
<span className={treeLeafSubInfoClassName}>
<Scrollable>
<div className={defaultClassName}>
<TreeView
className={treeClassName}
data={data}
renderTitle={({ children, name, value }, _, isLeaf) => {
return !isLeaf ? (
<span className={treeNodeClassName}>
{value.code}
<span className={treeNodeBadgeClassName}>
{children?.length}
</span>
</span>
<span className={treeLeafSubInfoClassName}>
[{value.startLineNumber}, {value.startColumn}]
) : (
<span className={treeLeafClassName}>
{value.message}
<span className={treeLeafSubInfoClassName}>
{value.code}
</span>
<span className={treeLeafSubInfoClassName}>
[{value.startLineNumber},{' '}
{value.startColumn}]
</span>
</span>
</span>
);
}}
/>
</div>
);
}}
/>
</div>
</Scrollable>
);
}

Expand Down
39 changes: 39 additions & 0 deletions stories/extensions/test/testPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,43 @@ export default class TestPane extends React.Component {
molecule.panel.appendOutput('Number: ' + Math.random() * 10 + '\n');
};

const updateProblem = () => {
const problems = molecule.problems.getState().data;
molecule.problems.addProblems({
id: (problems[problems.length - 1]?.id || 0) + 10,
name: 'text.tsx',
value: {
code: 'text.tsx',
message: '文件夹',
startLineNumber: 0,
startColumn: 1,
endLineNumber: 0,
endColumn: 1,
status: 1,
},
children: [
{
id: (problems[problems.length - 1]?.id || 0) + 1,
name: '0-1',
value: {
code: 'endLineNumber',
message: '语法错误',
startLineNumber: 0,
startColumn: 1,
endLineNumber: 0,
endColumn: 1,
status: 2,
},
children: [],
},
],
});
};

const clearProblems = () => {
molecule.problems.clearProblems();
};

const newEditor = function () {
const key = (Math.random() * 10 + 1).toFixed(2);
const tabData: IEditorTab = {
Expand Down Expand Up @@ -270,6 +307,8 @@ PARTITIONED BY (ds string) lifecycle 1000;
<Button onClick={addPanel}>Add Panel</Button>
<Button onClick={showHidePanel}>Show/Hide Panel</Button>
<Button onClick={updateOutput}>Update Output</Button>
<Button onClick={updateProblem}>Update Problem</Button>
<Button onClick={clearProblems}>Clear Problem</Button>
</div>
<div style={{ margin: '50px 20px' }}>
<h2>Notification:</h2>
Expand Down