Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/browser/commentGlyphWidget.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as nls from '../../../../nls.js';
7
import { Color } from '../../../../base/common/color.js';
8
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidgetPosition } from '../../../../editor/browser/editorBrowser.js';
9
import { IModelDecorationOptions, OverviewRulerLane } from '../../../../editor/common/model.js';
10
import { ModelDecorationOptions } from '../../../../editor/common/model/textModel.js';
11
import { darken, editorBackground, editorForeground, listInactiveSelectionBackground, opaque, registerColor } from '../../../../platform/theme/common/colorRegistry.js';
12
import { themeColorFromId } from '../../../../platform/theme/common/themeService.js';
13
import { IEditorDecorationsCollection } from '../../../../editor/common/editorCommon.js';
14
import { CommentThreadState } from '../../../../editor/common/languages.js';
15
import { Disposable, toDisposable } from '../../../../base/common/lifecycle.js';
16
import { Emitter } from '../../../../base/common/event.js';
17
18
export const overviewRulerCommentingRangeForeground = registerColor('editorGutter.commentRangeForeground', { dark: opaque(listInactiveSelectionBackground, editorBackground), light: darken(opaque(listInactiveSelectionBackground, editorBackground), .05), hcDark: Color.white, hcLight: Color.black }, nls.localize('editorGutterCommentRangeForeground', 'Editor gutter decoration color for commenting ranges. This color should be opaque.'));
19
const overviewRulerCommentForeground = registerColor('editorOverviewRuler.commentForeground', overviewRulerCommentingRangeForeground, nls.localize('editorOverviewRuler.commentForeground', 'Editor overview ruler decoration color for resolved comments. This color should be opaque.'));
20
const overviewRulerCommentUnresolvedForeground = registerColor('editorOverviewRuler.commentUnresolvedForeground', overviewRulerCommentForeground, nls.localize('editorOverviewRuler.commentUnresolvedForeground', 'Editor overview ruler decoration color for unresolved comments. This color should be opaque.'));
21
22
const editorGutterCommentGlyphForeground = registerColor('editorGutter.commentGlyphForeground', { dark: editorForeground, light: editorForeground, hcDark: Color.black, hcLight: Color.white }, nls.localize('editorGutterCommentGlyphForeground', 'Editor gutter decoration color for commenting glyphs.'));
23
registerColor('editorGutter.commentUnresolvedGlyphForeground', editorGutterCommentGlyphForeground, nls.localize('editorGutterCommentUnresolvedGlyphForeground', 'Editor gutter decoration color for commenting glyphs for unresolved comment threads.'));
24
25
export class CommentGlyphWidget extends Disposable {
26
public static description = 'comment-glyph-widget';
27
private _lineNumber!: number;
28
private _editor: ICodeEditor;
29
private _threadState: CommentThreadState | undefined;
30
private readonly _commentsDecorations: IEditorDecorationsCollection;
31
private _commentsOptions: ModelDecorationOptions;
32
33
private readonly _onDidChangeLineNumber = this._register(new Emitter<number>());
34
public readonly onDidChangeLineNumber = this._onDidChangeLineNumber.event;
35
36
constructor(editor: ICodeEditor, lineNumber: number) {
37
super();
38
this._commentsOptions = this.createDecorationOptions();
39
this._editor = editor;
40
this._commentsDecorations = this._editor.createDecorationsCollection();
41
this._register(this._commentsDecorations.onDidChange(e => {
42
const range = (this._commentsDecorations.length > 0 ? this._commentsDecorations.getRange(0) : null);
43
if (range && range.endLineNumber !== this._lineNumber) {
44
this._lineNumber = range.endLineNumber;
45
this._onDidChangeLineNumber.fire(this._lineNumber);
46
}
47
}));
48
this._register(toDisposable(() => this._commentsDecorations.clear()));
49
this.setLineNumber(lineNumber);
50
}
51
52
private createDecorationOptions(): ModelDecorationOptions {
53
const unresolved = this._threadState === CommentThreadState.Unresolved;
54
const decorationOptions: IModelDecorationOptions = {
55
description: CommentGlyphWidget.description,
56
isWholeLine: true,
57
overviewRuler: {
58
color: themeColorFromId(unresolved ? overviewRulerCommentUnresolvedForeground : overviewRulerCommentForeground),
59
position: OverviewRulerLane.Center
60
},
61
collapseOnReplaceEdit: true,
62
linesDecorationsClassName: `comment-range-glyph comment-thread${unresolved ? '-unresolved' : ''}`
63
};
64
65
return ModelDecorationOptions.createDynamic(decorationOptions);
66
}
67
68
setThreadState(state: CommentThreadState | undefined): void {
69
if (this._threadState !== state) {
70
this._threadState = state;
71
this._commentsOptions = this.createDecorationOptions();
72
this._updateDecorations();
73
}
74
}
75
76
private _updateDecorations(): void {
77
const commentsDecorations = [{
78
range: {
79
startLineNumber: this._lineNumber, startColumn: 1,
80
endLineNumber: this._lineNumber, endColumn: 1
81
},
82
options: this._commentsOptions
83
}];
84
85
this._commentsDecorations.set(commentsDecorations);
86
}
87
88
setLineNumber(lineNumber: number): void {
89
this._lineNumber = lineNumber;
90
this._updateDecorations();
91
}
92
93
getPosition(): IContentWidgetPosition {
94
const range = (this._commentsDecorations.length > 0 ? this._commentsDecorations.getRange(0) : null);
95
96
return {
97
position: {
98
lineNumber: range ? range.endLineNumber : this._lineNumber,
99
column: 1
100
},
101
preference: [ContentWidgetPositionPreference.EXACT]
102
};
103
}
104
}
105
106