Skip to content

Commit 92f09d3

Browse files
committed
Fix duplicate error icon in tooltip
1 parent 8e936b6 commit 92f09d3

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

packages/editor-codemirror/src/behaviors/diagnostics.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ import { CodeViewCellContext, codeViewCellContext, kEndColumn, kEndRow, kStartCo
2727
import { lines } from "core";
2828
import { Position } from "vscode-languageserver-types";
2929

30-
const EMPTY_SELECTION = { start: Position.create(0, 0), end: Position.create(0, 0) }
30+
const EMPTY_CODEVIEW_SELECTION = { start: Position.create(0, 0), end: Position.create(0, 0) }
31+
32+
const ERROR_ICON_HTML_STR = `<span style="color: red; font-size: 24px; vertical-align: text-bottom; padding-right: 8px;">⚠︎</span>`
33+
const ERROR_TOOLTIP_DIV_STYLES = {
34+
"box-shadow": 'rgba(0, 0, 0, 0.16) 0px 0px 8px 2px',
35+
border: '1px solid lightgrey',
36+
padding: '4px 11px',
37+
"font-family": 'monospace'
38+
}
3139

3240
export function diagnosticsBehavior(behaviorContext: BehaviorContext): Behavior {
3341
// don't provide behavior if we don't have validation
@@ -36,7 +44,7 @@ export function diagnosticsBehavior(behaviorContext: BehaviorContext): Behavior
3644
}
3745

3846
return {
39-
extensions: [underlinedDrrorHoverTooltip],
47+
extensions: [underlinedErrorHoverTooltip],
4048

4149
async init(pmNode, cmView) {
4250
const language = behaviorContext.options.lang(pmNode, pmNode.textContent)
@@ -56,19 +64,15 @@ export function diagnosticsBehavior(behaviorContext: BehaviorContext): Behavior
5664
code: code.map(line => !/^(---|\.\.\.)\s*$/.test(line) ? line : ""),
5765
cellBegin: 0,
5866
cellEnd: code.length - 1,
59-
selection: EMPTY_SELECTION
67+
selection: EMPTY_CODEVIEW_SELECTION
6068
}
6169

6270
const diagnostics = await getDiagnostics(cellContext, behaviorContext)
6371
if (!diagnostics) return
6472

6573
for (const error of diagnostics) {
6674
underline(cmView,
67-
// Note: strangely, `error[kStartColumn]` gives the text editor *row* index and vice versa;
68-
// so we have to pass `error[kStartColumn]` as row and vice versa.
69-
// Note: to get the correct index, `code` must not have delimiters stripped out
7075
rowColumnToIndex(code, [error[kStartColumn], error[kStartRow]]),
71-
// same here
7276
rowColumnToIndex(code, [error[kEndColumn], error[kEndRow]]),
7377
error.text
7478
)
@@ -102,17 +106,12 @@ export function diagnosticsBehavior(behaviorContext: BehaviorContext): Behavior
102106
const diagnostics = await getDiagnostics(cellContext, behaviorContext)
103107
if (!diagnostics) return
104108

105-
console.log('UPDATE DEBUG!!', cellContext)
106-
107109
const codeLines = lines(updatePmNode.textContent)
108110
for (const error of diagnostics) {
109111
underline(cmView,
110-
// strangely, `error[kStartColumn]` gives the visual *row* index and vice versa
111112
rowColumnToIndex(codeLines, [error[kStartColumn], error[kStartRow]]),
112113
rowColumnToIndex(codeLines, [error[kEndColumn], error[kEndRow]]),
113-
'<div style="padding: 4px 11px; font-family: monospace;"><span style="color: red; font-size: 24px; vertical-align: text-bottom;">⚠︎</span> ' +
114-
error.text +
115-
'</div>'
114+
error.text
116115
)
117116
}
118117
}
@@ -131,7 +130,7 @@ async function getDiagnostics(
131130

132131
//Check if there is an underline at position and display a tooltip there
133132
//We want to show the error message as well
134-
const underlinedDrrorHoverTooltip = hoverTooltip((view, pos) => {
133+
const underlinedErrorHoverTooltip = hoverTooltip((view, pos) => {
135134
const f = view.state.field(underlineField, false)
136135
if (!f) return null
137136

@@ -144,14 +143,14 @@ const underlinedDrrorHoverTooltip = hoverTooltip((view, pos) => {
144143
end: to,
145144
above: true,
146145
create() {
147-
let dom = document.createElement("div")
148-
Object.assign(dom.style, {
149-
"box-shadow": 'rgba(0, 0, 0, 0.16) 0px 0px 8px 2px',
150-
border: '1px solid lightgrey'
151-
})
152-
dom.innerHTML = '<div style="padding: 4px 11px; font-family: monospace;"><span style="color: red; font-size: 24px; vertical-align: text-bottom;">⚠︎</span> ' +
153-
spec.message +
154-
'</div>'
146+
const dom = document.createElement("div")
147+
Object.assign(dom.style, ERROR_TOOLTIP_DIV_STYLES)
148+
dom.innerHTML = ERROR_ICON_HTML_STR
149+
150+
const messageSpanEl = document.createElement("span")
151+
messageSpanEl.innerText = spec.message
152+
dom.append(messageSpanEl)
153+
155154
return { dom }
156155
}
157156
}
@@ -229,6 +228,10 @@ const clearUnderlines = (cmView: EditorView) => {
229228
cmView.dispatch({ effects: [removeUnderlines.of()] })
230229
}
231230

231+
//----------------
232+
// HELPERS
233+
//----------------
234+
232235
// helper function for positionally picking data from a DecorationSet
233236
const rangeAndSpecOfDecorationAtPos = (pos: number, d: DecorationSet) => {
234237
let spec: any | undefined
@@ -246,10 +249,15 @@ const rangeAndSpecOfDecorationAtPos = (pos: number, d: DecorationSet) => {
246249
return spec !== undefined ? { range: { from: from!, to: to! }, spec } : undefined
247250
}
248251

249-
function rowColumnToIndex(strs: string[], [row, col]: [number, number]): number {
252+
/**
253+
* @param strs A representation of a string, split by newlines.
254+
* @param [row, col] row and column into the string, row being the same as line number
255+
* @returns An index into the string i.e. An index into `strs.join('\n')`
256+
*/
257+
function rowColumnToIndex(strs: string[], [col, row]: [number, number]): number {
250258
let index = 0
251-
for (let i = 0; i < col; i++) {
259+
for (let i = 0; i < row; i++) {
252260
index += strs[i].length + 1
253261
}
254-
return index + row
262+
return index + col
255263
}

0 commit comments

Comments
 (0)