Place diff note popover below annotated line (#1948)
* Place diff note popover below annotated line * Remove diff note popover design doc
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { editor as monacoEditor } from 'monaco-editor'
|
||||
import { getDiffCommentPopoverTop } from './diff-comment-popover-position'
|
||||
|
||||
function makeEditor({
|
||||
lineCount = 10,
|
||||
scrollTop = 15,
|
||||
topForLine = (lineNumber: number) => lineNumber * 20
|
||||
}: {
|
||||
lineCount?: number
|
||||
scrollTop?: number
|
||||
topForLine?: (lineNumber: number) => number
|
||||
} = {}): Parameters<typeof getDiffCommentPopoverTop>[0] {
|
||||
return {
|
||||
getModel: () => ({ getLineCount: () => lineCount }) as monacoEditor.ITextModel,
|
||||
getScrollTop: () => scrollTop,
|
||||
getTopForLineNumber: topForLine
|
||||
}
|
||||
}
|
||||
|
||||
describe('getDiffCommentPopoverTop', () => {
|
||||
it('positions the popover below the anchor line', () => {
|
||||
const top = getDiffCommentPopoverTop(makeEditor(), 3, 20)
|
||||
|
||||
expect(top).toBe(65)
|
||||
})
|
||||
|
||||
it('uses a fallback line height when Monaco does not return a positive number', () => {
|
||||
const top = getDiffCommentPopoverTop(makeEditor(), 3, 0)
|
||||
|
||||
expect(top).toBe(64)
|
||||
})
|
||||
|
||||
it('returns null when the editor has no model', () => {
|
||||
const editor = {
|
||||
...makeEditor(),
|
||||
getModel: () => null
|
||||
}
|
||||
|
||||
expect(getDiffCommentPopoverTop(editor, 3, 20)).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for out-of-range line numbers', () => {
|
||||
expect(getDiffCommentPopoverTop(makeEditor({ lineCount: 2 }), 3, 20)).toBeNull()
|
||||
expect(getDiffCommentPopoverTop(makeEditor({ lineCount: 2 }), 0, 20)).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { editor as monacoEditor } from 'monaco-editor'
|
||||
|
||||
type DiffCommentPopoverEditor = Pick<
|
||||
monacoEditor.ICodeEditor,
|
||||
'getModel' | 'getScrollTop' | 'getTopForLineNumber'
|
||||
>
|
||||
|
||||
const FALLBACK_LINE_HEIGHT_PX = 19
|
||||
|
||||
export function getDiffCommentPopoverTop(
|
||||
editor: DiffCommentPopoverEditor,
|
||||
lineNumber: number,
|
||||
lineHeight: unknown
|
||||
): number | null {
|
||||
const model = editor.getModel()
|
||||
if (!model) {
|
||||
return null
|
||||
}
|
||||
if (lineNumber < 1 || lineNumber > model.getLineCount()) {
|
||||
return null
|
||||
}
|
||||
const resolvedLineHeight =
|
||||
typeof lineHeight === 'number' && lineHeight > 0 ? lineHeight : FALLBACK_LINE_HEIGHT_PX
|
||||
return editor.getTopForLineNumber(lineNumber) - editor.getScrollTop() + resolvedLineHeight
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import type { editor as monacoEditor, IDisposable } from 'monaco-editor'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import type { DiffComment } from '../../../../shared/types'
|
||||
import { DiffCommentCard } from './DiffCommentCard'
|
||||
import { getDiffCommentPopoverTop } from './diff-comment-popover-position'
|
||||
|
||||
// Why: Monaco glyph-margin *decorations* don't expose click events in a way
|
||||
// that lets us show a polished popover anchored to a line. So instead we own a
|
||||
@@ -203,7 +204,10 @@ export function useDiffCommentDecorator({
|
||||
}
|
||||
const startLine = Math.min(currentDrag.startLine, currentDrag.endLine)
|
||||
const lineNumber = Math.max(currentDrag.startLine, currentDrag.endLine)
|
||||
const top = editor.getTopForLineNumber(lineNumber) - editor.getScrollTop()
|
||||
const top = getDiffCommentPopoverTop(editor, lineNumber, getLineHeight())
|
||||
if (top == null) {
|
||||
return
|
||||
}
|
||||
onAddCommentClickRef.current({
|
||||
lineNumber,
|
||||
startLine: startLine === lineNumber ? undefined : startLine,
|
||||
|
||||
@@ -19,6 +19,7 @@ import { computeEditorFontSize } from '@/lib/editor-font-zoom'
|
||||
import { findWorktreeById } from '@/store/slices/worktree-helpers'
|
||||
import { useDiffCommentDecorator } from '../diff-comments/useDiffCommentDecorator'
|
||||
import { DiffCommentPopover } from '../diff-comments/DiffCommentPopover'
|
||||
import { getDiffCommentPopoverTop } from '../diff-comments/diff-comment-popover-position'
|
||||
import { applyDiffEditorLineNumberOptions } from './diff-editor-line-number-options'
|
||||
import { computeLineStats } from './diff-line-stats'
|
||||
import type { DiffComment, GitDiffResult } from '../../../../shared/types'
|
||||
@@ -161,8 +162,15 @@ export function DiffSectionItem({
|
||||
return
|
||||
}
|
||||
const update = (): void => {
|
||||
const top =
|
||||
modifiedEditor.getTopForLineNumber(popover.lineNumber) - modifiedEditor.getScrollTop()
|
||||
const top = getDiffCommentPopoverTop(
|
||||
modifiedEditor,
|
||||
popover.lineNumber,
|
||||
modifiedEditor.getOption(monaco.editor.EditorOption.lineHeight)
|
||||
)
|
||||
if (top == null) {
|
||||
setPopover(null)
|
||||
return
|
||||
}
|
||||
setPopover((prev) => (prev ? { ...prev, top } : prev))
|
||||
}
|
||||
const scrollSub = modifiedEditor.onDidScrollChange(update)
|
||||
|
||||
@@ -3,12 +3,13 @@ import { DiffEditor, type DiffOnMount } from '@monaco-editor/react'
|
||||
import type { editor } from 'monaco-editor'
|
||||
import { useAppStore } from '@/store'
|
||||
import { diffViewStateCache, setWithLRU } from '@/lib/scroll-cache'
|
||||
import '@/lib/monaco-setup'
|
||||
import { monaco } from '@/lib/monaco-setup'
|
||||
import { computeEditorFontSize } from '@/lib/editor-font-zoom'
|
||||
import { useContextualCopySetup } from './useContextualCopySetup'
|
||||
import { findWorktreeById } from '@/store/slices/worktree-helpers'
|
||||
import { useDiffCommentDecorator } from '../diff-comments/useDiffCommentDecorator'
|
||||
import { DiffCommentPopover } from '../diff-comments/DiffCommentPopover'
|
||||
import { getDiffCommentPopoverTop } from '../diff-comments/diff-comment-popover-position'
|
||||
import { applyDiffEditorLineNumberOptions } from './diff-editor-line-number-options'
|
||||
import type { DiffComment } from '../../../../shared/types'
|
||||
import { isDiffComment } from '@/lib/diff-comment-compat'
|
||||
@@ -129,8 +130,15 @@ export default function DiffViewer({
|
||||
return
|
||||
}
|
||||
const update = (): void => {
|
||||
const top =
|
||||
modifiedEditor.getTopForLineNumber(popover.lineNumber) - modifiedEditor.getScrollTop()
|
||||
const top = getDiffCommentPopoverTop(
|
||||
modifiedEditor,
|
||||
popover.lineNumber,
|
||||
modifiedEditor.getOption(monaco.editor.EditorOption.lineHeight)
|
||||
)
|
||||
if (top == null) {
|
||||
setPopover(null)
|
||||
return
|
||||
}
|
||||
setPopover((prev) => (prev ? { ...prev, top } : prev))
|
||||
}
|
||||
const scrollSub = modifiedEditor.onDidScrollChange(update)
|
||||
|
||||
Reference in New Issue
Block a user