Skip to content

Commit 6f41357

Browse files
committed
Cleanup a little more
1 parent be73a76 commit 6f41357

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ import { Decoration, DecorationSet } from "@codemirror/view";
2020
import { StateField, StateEffect } from "@codemirror/state";
2121
import { hoverTooltip } from "@codemirror/view";
2222

23-
import { CodeViewCellContext, codeViewCellContext, kEndColumn, kEndRow, kStartColumn, kStartRow, LintItem } from "editor";
23+
import {
24+
CodeViewCellContext,
25+
codeViewCellContext,
26+
kEndColumn,
27+
kEndRow,
28+
kStartColumn,
29+
kStartRow,
30+
LintItem,
31+
stripYamlFrontmatterDelimiters
32+
} from "editor";
2433
import { lines } from "core";
2534
import { Position } from "vscode-languageserver-types";
2635

@@ -52,13 +61,14 @@ export function diagnosticsBehavior(behaviorContext: BehaviorContext): Behavior
5261
if (filepath === null) return;
5362

5463
const code = lines(pmNode.textContent);
64+
const strippedCodeLines = stripYamlFrontmatterDelimiters(code);
5565

5666
// here we hand-craft an artisinal cellContext because `codeViewCellContext(..)`
5767
// seems to return undefined inside of init
5868
const cellContext = {
5969
filepath,
6070
language: 'yaml',
61-
code: code.map(line => !/^(---|\.\.\.)\s*$/.test(line) ? line : ""),
71+
code: strippedCodeLines,
6272
cellBegin: 0,
6373
cellEnd: code.length - 1,
6474
selection: EMPTY_CODEVIEW_SELECTION
@@ -102,10 +112,7 @@ async function getDiagnostics(
102112
cellContext: CodeViewCellContext,
103113
behaviorContext: BehaviorContext
104114
): Promise<LintItem[] | undefined> {
105-
const diagnostics = await behaviorContext.pmContext.ui.codeview?.codeViewDiagnostics(cellContext);
106-
if (!diagnostics) return undefined;
107-
108-
return diagnostics;
115+
return await behaviorContext.pmContext.ui.codeview?.codeViewDiagnostics(cellContext);
109116
}
110117

111118
//Check if there is an underline at position and display a tooltip there
@@ -205,14 +212,15 @@ const rangeAndSpecOfDecorationAtPos = (pos: number, d: DecorationSet) => {
205212
};
206213

207214
/**
208-
* @param strs A representation of a string, split by newlines.
215+
* @param lines A representation of a string, split by newlines.
209216
* @param [row, col] row and column into the string, row being the same as line number
210-
* @returns An index into the string i.e. An index into `strs.join('\n')`
217+
* @returns An index into the string i.e. An index into `lines.join('\n')`
211218
*/
212-
function rowColumnToIndex(strs: string[], [col, row]: [number, number]): number {
219+
function rowColumnToIndex(lines: string[], [col, row]: [number, number]): number {
213220
let index = 0;
214221
for (let i = 0; i < row; i++) {
215-
index += strs[i].length + 1;
222+
// + 1 to account for the newline character
223+
index += lines[i].length + 1;
216224
}
217225
return index + col;
218226
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
import { Node as ProsemirrorNode } from 'prosemirror-model'
19+
import { Node as ProsemirrorNode } from 'prosemirror-model';
2020
import { EditorView as PMEditorView } from "prosemirror-view";
2121

2222
import { Extension, Transaction } from "@codemirror/state";
@@ -52,7 +52,7 @@ export interface BehaviorContext {
5252
getPos: boolean | (() => number);
5353
options: CodeViewOptions;
5454
pmContext: ExtensionContext;
55-
withState: WithState
55+
withState: WithState;
5656
}
5757

5858
export enum State { Updating, Escaping };

packages/editor/src/api/codeview.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ export function codeViewCompletionContext(filepath: string, state: EditorState,
347347
}
348348
}
349349

350+
export function stripYamlFrontmatterDelimiters(lines: string[]): string[] {
351+
return lines.map(line => !/^(---|\.\.\.)\s*$/.test(line) ? line : "");
352+
}
353+
350354
export function codeViewCellContext(filepath: string, state: EditorState): CodeViewCellContext | undefined {
351355

352356
// get blocks (for active language only)
@@ -356,7 +360,7 @@ export function codeViewCellContext(filepath: string, state: EditorState): CodeV
356360
// if this is yaml we strip the delimiters and use only the active block
357361
if (activeBlockContext.activeLanguage === "yaml") {
358362
const activeBlock = activeBlockContext.blocks.find(block => block.active) || activeBlockContext.blocks[0];
359-
const codeLines = lines(activeBlock.code).map(line => !/^(---|\.\.\.)\s*$/.test(line) ? line : "");
363+
const codeLines = stripYamlFrontmatterDelimiters(lines(activeBlock.code));
360364
return {
361365
filepath,
362366
language: activeBlockContext.activeLanguage,

0 commit comments

Comments
 (0)