|
| 1 | +import { from } from 'rxjs' |
| 2 | +import { filter, switchMap } from 'rxjs/operators' |
1 | 3 | import * as sourcegraph from 'sourcegraph'
|
2 | 4 |
|
3 |
| -export function activate(ctx: sourcegraph.ExtensionContext): void { |
4 |
| - ctx.subscriptions.add( |
5 |
| - sourcegraph.languages.registerHoverProvider(['*'], { |
6 |
| - provideHover: () => ({ |
7 |
| - contents: { |
8 |
| - value: 'Hello world from sourcegraph-sentry! 🎉🎉🎉', |
9 |
| - kind: sourcegraph.MarkupKind.Markdown |
10 |
| - } |
11 |
| - }), |
12 |
| - }) |
13 |
| - ) |
| 5 | +const CODE_PATTERNS = [ |
| 6 | + /throw new Error+\([\'\"]([^\'\"]+)[\'\"]\)/gi, |
| 7 | + /console\.[^\'\"]+\([\'\"]([^\'\"]+)[\'\"]\)/gi, |
| 8 | + /log\.[^\'\"]+\([\'\"]([^\'\"]+)[\'\"]\)/gi, |
| 9 | +] |
| 10 | + |
| 11 | +const DECORATION_TYPE = sourcegraph.app.createDecorationType() |
| 12 | + |
| 13 | +function decorateEditor(editor: sourcegraph.CodeEditor): void { |
| 14 | + const decorations: sourcegraph.TextDocumentDecoration[] = [] |
| 15 | + for (const [i, line] of editor.document.text!.split('\n').entries()) { |
| 16 | + for (const pattern of CODE_PATTERNS) { |
| 17 | + const match = line.match(pattern) |
| 18 | + if (match) { |
| 19 | + decorations.push({ |
| 20 | + range: new sourcegraph.Range(i, 0, i, 0), |
| 21 | + isWholeLine: true, |
| 22 | + after: { |
| 23 | + backgroundColor: '#e03e2f', |
| 24 | + color: 'rgba(255, 255, 255, 0.8)', |
| 25 | + contentText: ' View logs in Sentry » ', |
| 26 | + linkURL: 'testUrl', |
| 27 | + }, |
| 28 | + }) |
| 29 | + } |
| 30 | + pattern.lastIndex = 0 // reset |
| 31 | + } |
| 32 | + } |
| 33 | + editor.setDecorations(DECORATION_TYPE, decorations) |
| 34 | +} |
| 35 | + |
| 36 | +export function activate(context: sourcegraph.ExtensionContext): void { |
| 37 | + if (sourcegraph.app.activeWindowChanges) { |
| 38 | + const activeEditor = from(sourcegraph.app.activeWindowChanges).pipe( |
| 39 | + filter((window): window is sourcegraph.Window => window !== undefined), |
| 40 | + switchMap(window => window.activeViewComponentChanges), |
| 41 | + filter((editor): editor is sourcegraph.CodeEditor => editor !== undefined) |
| 42 | + ) |
| 43 | + // When the active editor changes, publish new decorations. |
| 44 | + context.subscriptions.add(activeEditor.subscribe(decorateEditor)) |
| 45 | + } |
14 | 46 | }
|
15 | 47 |
|
16 | 48 | // Sourcegraph extension documentation: https://docs.sourcegraph.com/extensions/authoring
|
0 commit comments