Skip to content

Commit f502adb

Browse files
committed
feat: display the editor line and column info on statusBar
1 parent 8e6b1af commit f502adb

File tree

5 files changed

+97
-52
lines changed

5 files changed

+97
-52
lines changed

src/controller/editor.ts src/controller/editor.tsx

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import * as React from 'react';
12
import { EditorEvent, IEditorTab } from 'mo/model/workbench/editor';
23
import { Controller } from 'mo/react/controller';
3-
import { editorService } from 'mo/services';
4+
import { editorService, statusBarService } from 'mo/services';
45
import { singleton } from 'tsyringe';
6+
import * as monaco from 'monaco-editor';
7+
import { editorLineColumnItem } from './statusBar';
8+
59
export interface IEditorController {
610
groupSplitPos?: string[];
711
onCloseAll?: (group: number) => void;
@@ -14,10 +18,11 @@ export interface IEditorController {
1418
onTabContextMenu?: (e: React.MouseEvent, tab: IEditorTab) => void;
1519
}
1620

21+
type IStandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
22+
1723
@singleton()
1824
export class EditorController extends Controller implements IEditorController {
19-
private editorInstance;
20-
// Group Pos locate here temporary, we can move it to state or localStorage if you need
25+
// Group Pos locate here temporary, we can move it to state or localStorage in future.
2126
public groupSplitPos: string[] = [];
2227

2328
constructor() {
@@ -51,7 +56,7 @@ export class EditorController extends Controller implements IEditorController {
5156
};
5257

5358
public onUpdateEditorIns = (
54-
editorInstance: IStandaloneCodeEditor,
59+
editorInstance: monaco.editor.IStandaloneCodeEditor,
5560
groupId: number
5661
) => {
5762
if (editorInstance) {
@@ -79,7 +84,6 @@ export class EditorController extends Controller implements IEditorController {
7984
editorInstance: IStandaloneCodeEditor,
8085
groupId: number
8186
) {
82-
this.editorInstance = editorInstance;
8387
if (editorInstance) {
8488
editorInstance.onDidChangeModelContent((event: any) => {
8589
const newValue = editorInstance.getValue();
@@ -97,19 +101,47 @@ export class EditorController extends Controller implements IEditorController {
97101
},
98102
groupId
99103
);
104+
this.updateStatusBar(editorInstance);
100105
}
101106
});
102107

103108
editorInstance.onDidFocusEditorText(() => {
104109
const group = editorService.getGroupById(groupId);
105110
if (group?.tab!.id) {
106111
editorService.setActive(groupId, group.tab.id);
112+
this.updateEditorLineColumnInfo(editorInstance);
107113
}
108114
});
115+
116+
editorInstance.onDidChangeCursorSelection(() => {
117+
this.updateEditorLineColumnInfo(editorInstance);
118+
});
109119
}
110120
}
111121

112-
getEditorInstance() {
113-
return this.editorInstance;
122+
private updateStatusBar(editorInstance: IStandaloneCodeEditor) {
123+
if (editorInstance) {
124+
const model:
125+
| monaco.editor.ITextModel
126+
| null
127+
| undefined = editorInstance?.getModel();
128+
const decorations = model?.getAllDecorations();
129+
console.log('decorations:', decorations);
130+
}
131+
}
132+
133+
public updateEditorLineColumnInfo(editorInstance: IStandaloneCodeEditor) {
134+
if (editorInstance) {
135+
const position = editorInstance.getPosition();
136+
statusBarService.updateItem(
137+
Object.assign(editorLineColumnItem, {
138+
render: () => (
139+
<span>
140+
Ln {position?.lineNumber}, Col {position?.column}
141+
</span>
142+
),
143+
})
144+
);
145+
}
114146
}
115147
}

src/controller/statusBar.ts

-25
This file was deleted.

src/controller/statusBar.tsx

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as React from 'react';
2+
import { IStatusBarItem, StatusBarEvent } from 'mo';
3+
import { Controller } from 'mo/react/controller';
4+
import { statusBarService } from 'mo/services';
5+
import { singleton } from 'tsyringe';
6+
import { Icon } from 'mo/components/icon';
7+
8+
export interface IStatusBarController {
9+
onClick?: (e: React.MouseEvent, item: IStatusBarItem) => void;
10+
}
11+
12+
const problems: IStatusBarItem = {
13+
id: 'MoProblems',
14+
sortIndex: 1,
15+
name: 'Problems',
16+
};
17+
18+
const notifications: IStatusBarItem = {
19+
id: 'MoNotification',
20+
sortIndex: 1,
21+
name: 'Notification',
22+
render: () => <Icon type="bell" />,
23+
};
24+
25+
export const editorLineColumnItem: IStatusBarItem = {
26+
id: 'EditorCountInfo',
27+
sortIndex: 2,
28+
name: 'Go to Line/Column',
29+
render: () => <span>Ln 0, Col 0</span>,
30+
};
31+
32+
@singleton()
33+
export class StatusBarController
34+
extends Controller
35+
implements IStatusBarController {
36+
constructor() {
37+
super();
38+
this.initStatusBar();
39+
}
40+
41+
public onClick = (e: React.MouseEvent, item: IStatusBarItem) => {
42+
this.emit(StatusBarEvent.onClick, e, item);
43+
};
44+
45+
public notify() {
46+
console.log('service:', statusBarService);
47+
}
48+
49+
private initStatusBar() {
50+
statusBarService.appendLeftItem(problems);
51+
statusBarService.appendRightItem(notifications);
52+
statusBarService.appendRightItem(editorLineColumnItem);
53+
}
54+
}

src/extensions/statusBar/index.tsx

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
import * as React from 'react';
2-
import { IExtensionService, IStatusBarItem } from 'mo';
1+
// import * as React from 'react';
2+
import { IExtensionService } from 'mo';
33
import { IExtension } from 'mo/model/extension';
44
import { statusBarService } from 'mo/services';
5-
import { Icon } from 'mo/components/icon';
65

76
function init() {
8-
const problems: IStatusBarItem = {
9-
id: 'MoProblems',
10-
sortIndex: 1,
11-
name: 'Problems',
12-
};
13-
14-
const notifications: IStatusBarItem = {
15-
id: 'MoNotification',
16-
sortIndex: 1,
17-
name: 'Notification',
18-
render: () => <Icon type="bell" />,
19-
};
20-
21-
statusBarService.appendLeftItem(problems);
22-
statusBarService.appendRightItem(notifications);
23-
247
statusBarService.onClick(function (e, item) {
25-
console.log('statusBarService:', e, item, problems, notifications);
8+
console.log('statusBarService:', e, item);
269
});
2710
}
2811

src/services/workbench/statusBarService.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class StatusBarService
8787
const original = this.findById(item.id);
8888
if (original) {
8989
Object.assign(original, item);
90+
this.render();
9091
}
9192
}
9293
}

0 commit comments

Comments
 (0)