Skip to content

Commit 6f72e6b

Browse files
committed
Review
1 parent 8c31489 commit 6f72e6b

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed

MODULE.bazel.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/gui/src/project-view/components/MarkdownEditor/__tests__/blockFormatting.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { printTestInput, setupEditor } from '@/components/MarkdownEditor/__tests__/testInput'
22
import {
3-
canInsertCodeBlock,
43
getBlockType,
54
insertCodeBlock,
65
removeCodeBlock,
@@ -387,7 +386,6 @@ test.each([
387386
},
388387
])('Insert code block: $source', ({ source, expected }) => {
389388
const view = setupEditor(source)
390-
expect(canInsertCodeBlock(view.state)).toBe(true)
391389
view.dispatch(insertCodeBlock(view.state))
392390
expect(getBlockType(view.state)).toBe('FencedCode')
393391
expect(printTestInput(view.state.doc.toString(), view.state.selection.main)).toEqual(expected)

app/gui/src/project-view/components/MarkdownEditor/codemirror/formatting/block.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ export function setBlockType(
8686
}
8787
}
8888

89-
/**
90-
* @returns Whether {@link insertCodeBlock} is supported for the current cursor location or
91-
* selected range.
92-
*/
93-
export function canInsertCodeBlock(_state: EditorState): boolean {
94-
// TODO: Disable button when the cursor is already inside an unformattable block.
95-
return true
96-
}
97-
9889
/**
9990
* Insert a code block after the cursor, or if there is a selection convert the selected lines to a
10091
* code block.

app/gui/src/project-view/components/MarkdownEditor/codemirror/formatting/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/** @file Provides a Vue reactive API for Markdown formatting in CodeMirror. */
22
import {
3-
canInsertCodeBlock,
43
getBlockType,
54
insertCodeBlock,
65
removeCodeBlock,
@@ -24,6 +23,7 @@ export { type BlockType }
2423
interface ReactiveFormatting {
2524
inline: Record<InlineFormattingNode, Ref<boolean | undefined>>
2625
blockType: Ref<BlockType | undefined>
26+
unformattable: Ref<boolean>
2727
}
2828

2929
const reactiveFormattingFacet = Facet.define<ReactiveFormatting, ReactiveFormatting>({
@@ -47,8 +47,10 @@ export function useMarkdownFormatting(view: EditorView) {
4747
insertLink: computed(
4848
() => canInsertLink(view.state) && (() => view.dispatch(insertLink(view.state))),
4949
),
50-
insertCodeBlock: computed(
51-
() => canInsertCodeBlock(view.state) && (() => view.dispatch(insertCodeBlock(view.state))),
50+
insertCodeBlock: computed(() =>
51+
reactiveFormatting.unformattable.value ?
52+
undefined
53+
: () => view.dispatch(insertCodeBlock(view.state)),
5254
),
5355
blockType: proxyRefs({
5456
value: readonly(reactiveFormatting.blockType),
@@ -68,13 +70,14 @@ export function useMarkdownFormatting(view: EditorView) {
6870

6971
/** Returns an extension that supports reactively watch the formatting of the selected text. */
7072
export function markdownFormatting(): Extension {
71-
const reactiveFormatting = {
73+
const reactiveFormatting: ReactiveFormatting = {
7274
inline: {
73-
Emphasis: ref<boolean>(),
74-
StrongEmphasis: ref<boolean>(),
75-
Strikethrough: ref<boolean>(),
75+
Emphasis: ref(),
76+
StrongEmphasis: ref(),
77+
Strikethrough: ref(),
7678
},
77-
blockType: ref<BlockType | undefined>(),
79+
blockType: ref(),
80+
unformattable: ref(false),
7881
}
7982
const reactiveFormattingFacetExt = reactiveFormattingFacet.of(reactiveFormatting)
8083
return [
@@ -85,6 +88,7 @@ export function markdownFormatting(): Extension {
8588
for (const key of objects.unsafeKeys(reactiveFormatting.inline))
8689
reactiveFormatting.inline[key].value = formatting?.[key]
8790
reactiveFormatting.blockType.value = getBlockType(update.view.state)
91+
reactiveFormatting.unformattable.value = formatting === undefined
8892
}),
8993
]
9094
}

app/gui/src/project-view/components/MarkdownEditor/codemirror/formatting/inline.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export function setInlineFormatting(
5252
changes,
5353
// TODO SelectionMapping
5454
// `SelectionRange.map` produces a "valid" new selection based on the old selection and the
55-
// changes, but it isn't perfect. Once MDChangeBuilder's selection-adjusting logic is
56-
// consistently better than that sane default, we should switch to it and enable the checks of
57-
// after-edit selection boundaries `inlineFormatting.test.ts`.
55+
// changes, but it isn't perfect. Once MarkdownEdit's selection-adjusting logic is consistently
56+
// better than that sane default, we should switch to it and enable the checks of after-edit
57+
// selection boundaries `inlineFormatting.test.ts`.
5858
// selection: rangeToSelection(md.adjustedSelection),
5959
selection: state.selection.main.map(changes),
6060
}

app/lezer-markdown/src/markdown.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ function skipForList(bl: CompositeBlock, cx: BlockContext, line: Line) {
217217
const DefaultSkipMarkup: {[type: number]: (bl: CompositeBlock, cx: BlockContext, line: Line) => boolean} = {
218218
[Type.Blockquote](bl, cx, line) {
219219
if (line.next != 62 /* '>' */) return false
220-
let sAfter = space(line.text.charCodeAt(line.pos + 1)), size = sAfter ? 2 : 1
220+
let sAfter = space(line.text.charCodeAt(line.pos + 1))
221+
let size = sAfter ? 2 : 1
221222
line.markers.push(elt(Type.QuoteMark, cx.lineStart + line.pos, cx.lineStart + line.pos + (includeSpaceInDelimiterNode ? size : 1)))
222223
line.moveBase(line.pos + size)
223224
bl.end = cx.lineStart + line.text.length
@@ -484,17 +485,15 @@ const DefaultBlockParsers: {[name: string]: ((cx: BlockContext, line: Line) => B
484485
ATXHeading(cx, line) {
485486
let size = isAtxHeading(line)
486487
if (size < 0) return false
487-
let level = size
488-
if (includeSpaceInDelimiterNode && space(line.text.charCodeAt(size))) size += 1
489488
let off = line.pos, from = cx.lineStart + off
490489
let endOfSpace = skipSpaceBack(line.text, line.text.length, off), after = endOfSpace
491490
while (after > off && line.text.charCodeAt(after - 1) == line.next) after--
492491
if (after == endOfSpace || after == off || !space(line.text.charCodeAt(after - 1))) after = line.text.length
493492
let buf = cx.buffer
494-
.write(Type.HeaderMark, 0, size)
493+
.write(Type.HeaderMark, 0, size + (includeSpaceInDelimiterNode && space(line.text.charCodeAt(size)) ? 1 : 0))
495494
.writeElements(cx.parser.parseInline(line.text.slice(off + size + 1, after), from + size + 1), -from)
496495
if (after < line.text.length) buf.write(Type.HeaderMark, after - off, endOfSpace - off)
497-
let node = buf.finish(Type.ATXHeading1 - 1 + level, line.text.length - off)
496+
let node = buf.finish(Type.ATXHeading1 - 1 + size, line.text.length - off)
498497
cx.nextLine()
499498
cx.addNode(node, from)
500499
return true

app/ydoc-shared/src/ast/__tests__/ensoMarkdown.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ test.each([
104104
],
105105
not: ['Document', ['OrderedList', ['ListItem', ['ListMark', '1.'], ['Paragraph', 'Numbered']]]],
106106
},
107-
/*
108-
{ // FIXME
107+
{
109108
source: '# *Formatted header*',
110109
expected: [
111110
'Document',
@@ -116,7 +115,6 @@ test.each([
116115
],
117116
],
118117
},
119-
*/
120118
])('Syntax extension: Delimiter tokens include syntactic spaces: $source', checkTree)
121119

122120
// === "Incomplete" syntax special cases ===

0 commit comments

Comments
 (0)