Skip to content

Commit 6f8c800

Browse files
author
Deyan Totev
committed
feat: start sycn with origin
1 parent ff2c6cb commit 6f8c800

21 files changed

+1977
-235
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Differences:
1010
- using latest dependencies - main reason for the fork was to be able to update dependencies
1111
- show complexity number inside the decoration; no need to hover to see the number
1212
- different color sets depending if current theme is dark or light
13-
- instead of 4 levels of color decoration, there are around 25 levels.
1413
- no lua/vue/html support
1514
- no second decoration, `CodeLensEnabled` is not used
1615

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,54 @@
5757
"type": "object",
5858
"title": "CodeMetrics configuration",
5959
"properties": {
60+
"codemetrics.basics.ComplexityColorLowLight": {
61+
"scope": "resource",
62+
"default": "#4bb14f",
63+
"description": "Color for the low complexity level",
64+
"type": "string"
65+
},
66+
"codemetrics.basics.ComplexityColorNormalLight": {
67+
"scope": "resource",
68+
"default": "#ffc208",
69+
"description": "Color for the normal complexity level",
70+
"type": "string"
71+
},
72+
"codemetrics.basics.ComplexityColorHighLight": {
73+
"scope": "resource",
74+
"default": "#f44034",
75+
"description": "Color for the high complexity level",
76+
"type": "string"
77+
},
78+
"codemetrics.basics.ComplexityColorExtremeLight": {
79+
"scope": "resource",
80+
"default": "#f1f",
81+
"description": "Color for the extreme complexity level",
82+
"type": "string"
83+
},
84+
"codemetrics.basics.ComplexityColorLow": {
85+
"scope": "resource",
86+
"default": "#4bb14f",
87+
"description": "Color for the low complexity level",
88+
"type": "string"
89+
},
90+
"codemetrics.basics.ComplexityColorNormal": {
91+
"scope": "resource",
92+
"default": "#ffc208",
93+
"description": "Color for the normal complexity level",
94+
"type": "string"
95+
},
96+
"codemetrics.basics.ComplexityColorHigh": {
97+
"scope": "resource",
98+
"default": "#f44034",
99+
"description": "Color for the high complexity level",
100+
"type": "string"
101+
},
102+
"codemetrics.basics.ComplexityColorExtreme": {
103+
"scope": "resource",
104+
"default": "#f1f",
105+
"description": "Color for the extreme complexity level",
106+
"type": "string"
107+
},
60108
"codemetrics.basics.Exclude": {
61109
"scope": "resource",
62110
"description": "Array of exclusion patterns (in glob format)",

src-forked/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MIN_COMPLEXITY = 0;
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import {
2+
TextEditorDecorationType,
3+
Disposable,
4+
ExtensionContext,
5+
workspace,
6+
window,
7+
DecorationOptions,
8+
TextEditor,
9+
Uri,
10+
DecorationRenderOptions,
11+
OverviewRulerLane,
12+
DecorationRangeBehavior,
13+
} from 'vscode'
14+
import {MetricsUtil} from '../metrics/MetricsUtil'
15+
import {IVSCodeMetricsConfiguration} from '../metrics/common/VSCodeMetricsConfiguration'
16+
import {getColor} from './get-color'
17+
import {interpolate, toDecimal} from 'rambdax'
18+
import { IMetricsModel } from '../tsmetrics-core/MetricsModel'
19+
20+
let decorationTemplateSimple = "<svg xmlns='http://www.w3.org/2000/svg' width='{{size}}px' height='{{size}}px' viewbox='0 0 {{size}} {{size}}'><rect width='{{size}}px' height='{{size}}px' style='fill:{{color}};stroke-width:1px;stroke:{{color}}'/></svg>"
21+
let decorationTemplateComplex = "<svg xmlns='http://www.w3.org/2000/svg' width='{{size}}px' height='{{size}}px' viewbox='0 0 {{size}} {{size}}'><rect width='{{size}}px' height='{{size}}px' style='fill:{{color}};stroke-width:1px;stroke:{{color}}'/><text dy='1px' x='50%' y='50%' dominant-baseline='middle' text-anchor='middle' style='fill:#fff;font-size:{{textSize}}px;'>{{complexity}}</text></svg>"
22+
23+
export class EditorDecoration implements Disposable {
24+
private decoratorInstances: TextEditorDecorationType[] = []
25+
private decorationModeEnabled: boolean = false
26+
private overviewRulerModeEnabled: boolean = false
27+
private low: TextEditorDecorationType;
28+
private normal: TextEditorDecorationType;
29+
private high: TextEditorDecorationType;
30+
private extreme: TextEditorDecorationType;
31+
32+
private metricsUtil: MetricsUtil
33+
private onDidSaveTextDocument: Disposable
34+
private didOpenTextDocument: Disposable
35+
constructor(context: ExtensionContext, metricsUtil: MetricsUtil) {
36+
this.metricsUtil = metricsUtil
37+
38+
this.onDidSaveTextDocument = workspace.onDidSaveTextDocument(e => {
39+
this.update()
40+
})
41+
this.didOpenTextDocument = window.onDidChangeActiveTextEditor(e => {
42+
this.disposeDecorators()
43+
this.update()
44+
})
45+
this.update()
46+
}
47+
48+
private update() {
49+
const editor = window.activeTextEditor
50+
51+
if (!editor || !editor.document) {
52+
return
53+
}
54+
const document = editor.document
55+
const settings = this.metricsUtil.appConfig.getCodeMetricsSettings(
56+
document.uri
57+
)
58+
59+
const languageDisabled =
60+
this.metricsUtil.selector.filter(
61+
s => s.language == document.languageId
62+
).length == 0
63+
const decorationDisabled = !(
64+
settings.DecorationModeEnabled || settings.OverviewRulerModeEnabled
65+
)
66+
if (decorationDisabled || languageDisabled) {
67+
this.clearDecorators(editor)
68+
return
69+
}
70+
// for some reason the context is lost
71+
var thisContext = this
72+
const size: number = workspace
73+
.getConfiguration('editor', document.uri)
74+
.get('fontSize')
75+
this.metricsUtil.getMetrics(document).then(
76+
metrics => {
77+
if (
78+
thisContext.settingsChanged(settings) ||
79+
this.decoratorInstances.length === 0
80+
) {
81+
thisContext.clearDecorators(editor)
82+
}
83+
const toDecoration = (model: IMetricsModel): DecorationOptions => {
84+
return {
85+
hoverMessage: model.toString(),
86+
range: thisContext.metricsUtil.toDecorationRange(
87+
model.start,
88+
document
89+
),
90+
}
91+
}
92+
const complexityAndModel: ComplexityToModel[] = metrics.map(p => {
93+
return {complexity: p.getCollectedComplexity(), model: p}
94+
})
95+
96+
const decorations = complexityAndModel.map(p => ({
97+
decorationStyle: toDecoration(p.model),
98+
complexity: p.complexity,
99+
}))
100+
101+
// need to reset all decorations before setting new ones
102+
thisContext.decoratorInstances.forEach(decorator => {
103+
editor.setDecorations(decorator, [])
104+
})
105+
106+
const decoratorInstances = decorations.map(
107+
({decorationStyle, complexity}) => {
108+
const decoration = this.createDecorationType(
109+
settings.DecorationModeEnabled,
110+
settings.OverviewRulerModeEnabled,
111+
getColor(complexity),
112+
size,
113+
complexity
114+
)
115+
editor.setDecorations(decoration, [decorationStyle])
116+
117+
return decoration
118+
}
119+
)
120+
thisContext.decoratorInstances = decoratorInstances
121+
},
122+
e => {
123+
var exmsg = ''
124+
if (e.message) {
125+
exmsg += e.message
126+
}
127+
if (e.stack) {
128+
exmsg += ' | stack: ' + e.stack
129+
}
130+
console.error(exmsg)
131+
}
132+
)
133+
}
134+
private settingsChanged(settings: IVSCodeMetricsConfiguration): boolean {
135+
const changed =
136+
settings.DecorationModeEnabled != this.decorationModeEnabled ||
137+
settings.OverviewRulerModeEnabled != this.overviewRulerModeEnabled
138+
this.decorationModeEnabled = settings.DecorationModeEnabled
139+
this.overviewRulerModeEnabled = settings.OverviewRulerModeEnabled
140+
return changed
141+
}
142+
private clearDecorators(editor: TextEditor) {
143+
if (this.decoratorInstances.length > 0) {
144+
this.decoratorInstances.forEach(decorator => {
145+
editor.setDecorations(decorator, [])
146+
})
147+
this.decoratorInstances = []
148+
}
149+
this.disposeDecorators()
150+
}
151+
152+
createDecorationType(
153+
decorationModeEnabled: boolean,
154+
overviewRulerModeEnabled: boolean,
155+
color: string,
156+
size: number,
157+
complexity: number
158+
) {
159+
const options: DecorationRenderOptions = {
160+
overviewRulerLane: OverviewRulerLane.Right,
161+
overviewRulerColor: color,
162+
before: {
163+
contentIconPath: this.getContentIconPath(
164+
color,
165+
size,
166+
complexity
167+
),
168+
margin: `${size / 2}px`,
169+
},
170+
}
171+
if (!decorationModeEnabled) {
172+
options.before = null
173+
}
174+
if (!overviewRulerModeEnabled) {
175+
options.overviewRulerColor = null
176+
}
177+
options.rangeBehavior = DecorationRangeBehavior.ClosedClosed
178+
return window.createTextEditorDecorationType(options)
179+
}
180+
getContentIconPath(
181+
color: string,
182+
size: number,
183+
complexity: number
184+
): Uri {
185+
const template = complexity > 12 ? decorationTemplateComplex : decorationTemplateSimple
186+
const textSize = toDecimal(size * 0.85)
187+
const decoration = interpolate(template, {color, size, complexity, textSize})
188+
189+
return Uri.parse(`data:image/svg+xml,` + encodeURIComponent(decoration))
190+
}
191+
disposeDecorators() {
192+
if (this.decoratorInstances.length > 0) {
193+
this.decoratorInstances.forEach(decorator => {
194+
decorator.dispose()
195+
})
196+
}
197+
this.decoratorInstances = []
198+
}
199+
200+
public dispose(): void {
201+
this.disposeDecorators()
202+
this.onDidSaveTextDocument.dispose()
203+
this.didOpenTextDocument.dispose()
204+
}
205+
}
206+
207+
interface ComplexityToModel {
208+
complexity: number
209+
model: IMetricsModel
210+
}
File renamed without changes.

src-forked/extension.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ExtensionContext } from "vscode";
2+
import { AppConfiguration } from "./models/AppConfiguration";
3+
import { MetricsUtil } from "./metrics/MetricsUtil";
4+
import { EditorDecoration } from "./editordecoration/EditorDecoration";
5+
6+
export function activate(context: ExtensionContext) {
7+
const config: AppConfiguration = new AppConfiguration();
8+
const metricsUtil: MetricsUtil = new MetricsUtil(config, context);
9+
10+
context.subscriptions.push(new EditorDecoration(context, metricsUtil));
11+
}

0 commit comments

Comments
 (0)