|
| 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 | +} |
0 commit comments