Move source editor out of module (#12457)

* Update Copybara options in preparation for open-sourcing the source editor

* Move files

* Update paths

* Remove source-editor module and checks for its existence

* Explicitly mention CM6 license in files that contain code adapted from CM6

GitOrigin-RevId: 89b7cc2b409db01ad103198ccbd1b126ab56349b
This commit is contained in:
Tim Down
2023-04-13 08:40:56 +00:00
committed by Copybot
parent 3f992f9453
commit 7f37ba737c
296 changed files with 33268 additions and 54 deletions
@@ -0,0 +1,244 @@
import { ensureSyntaxTree, syntaxTree } from '@codemirror/language'
import { EditorSelection, EditorState, SelectionRange } from '@codemirror/state'
import { SyntaxNode, Tree } from '@lezer/common'
import { isUnknownCommandWithName } from './common'
import { ListEnvironment } from '../../lezer-latex/latex.terms.mjs'
const HUNDRED_MS = 100
export type AncestorItem = {
node: SyntaxNode
label: string
type?: string
from: number
to: number
}
/**
* Get the stack of 'ancestor' nodes at the given position.
* The first element is the most distant ancestor, while the last element
* is the node at the position.
*/
export function getAncestorStack(
state: EditorState,
pos: number
): AncestorItem[] | null {
const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
if (!tree) {
return null
}
const stack: AncestorItem[] = []
const selectedNode = tree.resolve(pos, 0)
let node: SyntaxNode | null = selectedNode
while (node) {
const name = node.type.name
switch (name) {
case 'Environment':
{
const data: AncestorItem = {
node,
label: name,
from: node.from,
to: node.to,
}
const child = node.getChild('EnvNameGroup')
if (child) {
data.type = state.doc.sliceString(child.from + 1, child.to - 1)
}
stack.push(data)
}
break
default:
stack.push({ node, label: name, from: node.from, to: node.to })
break
}
node = node.parent
}
return stack.reverse()
}
export const ancestorNodeOfType = (
state: EditorState,
pos: number,
type: string | number,
side: -1 | 0 | 1 = 0
): SyntaxNode | null => {
const node: SyntaxNode | null = syntaxTree(state).resolveInner(pos, side)
return ancestorOfNodeWithType(node, type)
}
export function* ancestorsOfNodeWithType(
node: SyntaxNode | null,
type: string | number
): Generator<SyntaxNode> {
for (let ancestor = node; ancestor; ancestor = ancestor.parent) {
if (ancestor.type.is(type)) {
yield ancestor
}
}
}
export const ancestorOfNodeWithType = (
node: SyntaxNode | null | undefined,
...types: (string | number)[]
): SyntaxNode | null => {
for (let ancestor = node; ancestor; ancestor = ancestor.parent) {
for (const type of types) {
if (ancestor.type.is(type)) {
return ancestor
}
}
}
return null
}
export const descendantsOfNodeWithType = (
node: SyntaxNode,
type: string | number,
nested = false
): SyntaxNode[] => {
const children: SyntaxNode[] = []
node.cursor().iterate(nodeRef => {
if (nodeRef.type.is(type)) {
children.push(nodeRef.node)
if (!nested) {
return false
}
}
})
return children
}
export const getBibkeyArgumentNode = (state: EditorState, pos: number) => {
return (
ancestorNodeOfType(state, pos, 'BibKeyArgument', -1) ??
ancestorNodeOfType(state, pos, 'BibKeyArgument')
)
}
export function* ancestorsOfSelectionWithType(
tree: Tree,
selection: EditorSelection,
type: string | number
) {
for (const range of selection.ranges) {
const node = tree.resolveInner(range.anchor)
for (const ancestor of ancestorsOfNodeWithType(node, type)) {
if (ancestor) {
yield ancestor
}
}
}
}
export const matchingAncestor = (
node: SyntaxNode,
predicate: (node: SyntaxNode) => boolean
) => {
for (
let ancestor: SyntaxNode | null | undefined = node;
ancestor;
ancestor = ancestor.parent
) {
if (predicate(ancestor)) {
return ancestor
}
}
return null
}
export const ancestorWithType = (
state: EditorState,
nodeType: string | number
) => {
const tree = syntaxTree(state)
const ancestors = ancestorsOfSelectionWithType(
tree,
state.selection,
nodeType
)
return ancestors.next().value
}
export const commonAncestor = (
nodeA: SyntaxNode,
nodeB: SyntaxNode
): SyntaxNode | null => {
let cursorA: SyntaxNode | null = nodeA
let cursorB: SyntaxNode | null = nodeB
while (cursorA && cursorB) {
if (cursorA === cursorB) {
return cursorA
}
if (cursorA.from < cursorB.from) {
cursorB = cursorB.parent
} else {
cursorA = cursorA.parent
}
}
return null
}
export const withinFormattingCommand = (state: EditorState) => {
const tree = syntaxTree(state)
return (command: string): boolean => {
const isFormattedText = (range: SelectionRange): boolean => {
const nodeLeft = tree.resolveInner(range.from, -1)
const formattingCommandLeft = matchingAncestor(nodeLeft, node =>
isUnknownCommandWithName(node, command, state)
)
if (!formattingCommandLeft) {
return false
}
// We need to check the other end of the selection, and ensure that they
// share a common formatting command ancestor
const nodeRight = tree.resolveInner(range.to, 1)
const ancestor = commonAncestor(formattingCommandLeft, nodeRight)
if (!ancestor) {
return false
}
const formattingAncestor = matchingAncestor(ancestor, node =>
isUnknownCommandWithName(node, command, state)
)
return Boolean(formattingAncestor)
}
return state.selection.ranges.every(isFormattedText)
}
}
export type ListEnvironmentName = 'itemize' | 'enumerate'
export const listDepthForNode = (node: SyntaxNode) => {
let depth = 0
for (const ancestor of ancestorsOfNodeWithType(node, ListEnvironment)) {
if (ancestor) {
depth++
}
}
return depth
}
export const minimumListDepthForSelection = (state: EditorState) => {
const depths = []
for (const range of state.selection.ranges) {
const tree = syntaxTree(state)
const node = tree.resolveInner(range.anchor)
depths.push(listDepthForNode(node))
}
return Math.min(...depths)
}
@@ -0,0 +1,119 @@
import { EditorState } from '@codemirror/state'
import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
import { getOptionalArgumentText } from './common'
import { NodeIntersectsChangeFn, ProjectionItem } from './projection'
/**
* A projection of a command in the document
*/
export class Command extends ProjectionItem {
title = ''
optionalArgCount = 0
requiredArgCount = 0
}
/**
* Extracts Command instances from the syntax tree.
* `\newcommand` and `\renewcommand` are treated specially
*/
export const enterNode = (
state: EditorState,
node: SyntaxNodeRef,
items: Command[],
nodeIntersectsChange: NodeIntersectsChangeFn
): any => {
if (node.type.is('NewCommand') || node.type.is('RenewCommand')) {
if (!nodeIntersectsChange(node.node)) {
// This should already be in `items`
return
}
let commandName = node.node.getChild('LiteralArgContent')
if (!commandName) {
commandName = node.node.getChild('Csname')
}
if (!commandName) {
return
}
const commandNameText = state.doc.sliceString(
commandName.from,
commandName.to
)
if (commandNameText.length < 1) {
return
}
const optionalArguments = node.node.getChildren('OptionalArgument')
let argCountNumber = 0
if (optionalArguments.length > 0) {
const argumentCountNode = optionalArguments[0]
const argCountText = getOptionalArgumentText(state, argumentCountNode)
if (argCountText) {
try {
argCountNumber = parseInt(argCountText, 10)
} catch (err) {}
}
}
const commandDefinitionHasOptionalArgument = optionalArguments.length === 2
if (commandDefinitionHasOptionalArgument && argCountNumber > 0) {
argCountNumber--
}
const thisCommand = {
line: state.doc.lineAt(node.from).number,
title: commandNameText,
from: node.from,
to: node.to,
optionalArgCount: commandDefinitionHasOptionalArgument ? 1 : 0,
requiredArgCount: argCountNumber,
}
items.push(thisCommand)
} else if (
node.type.is('UnknownCommand') ||
node.type.is('MathCommand') ||
node.type.is('KnownCommand')
) {
let commandNode: SyntaxNode | null = node.node
if (node.type.is('KnownCommand')) {
// KnownCommands are defined as
//
// KnownCommand {
// CommandName {
// CommandCtrlSeq [args]
// }
// }
// So for a KnownCommand, use the first child as the actual command node
commandNode = commandNode.firstChild
}
if (!commandNode) {
return
}
if (!nodeIntersectsChange(node.node)) {
// This should already be in `items`
return
}
const ctrlSeq = commandNode.getChild('$CtrlSeq')
if (!ctrlSeq) {
return
}
const optionalArguments = commandNode.getChildren('OptionalArgument')
const commandArguments = commandNode.getChildren('$Argument')
const text = state.doc.sliceString(ctrlSeq.from, ctrlSeq.to)
const thisCommand = {
line: state.doc.lineAt(commandNode.from).number,
title: text,
from: commandNode.from,
to: commandNode.to,
optionalArgCount: optionalArguments.length,
requiredArgCount: commandArguments.length,
}
items.push(thisCommand)
}
}
@@ -0,0 +1,96 @@
import { ensureSyntaxTree } from '@codemirror/language'
import { EditorState } from '@codemirror/state'
import { SyntaxNode } from '@lezer/common'
const HUNDRED_MS = 100
/**
* Does this Comment node look like '% {' ?
* */
export const commentIsOpenFold = (
node: SyntaxNode,
state: EditorState
): boolean => {
const content = state.doc.sliceString(node.from, node.to)
return !!content.match(/%\s*\{\s*/)
}
/**
* Does this Comment node look like '% }' ?
* */
export const commentIsCloseFold = (
node: SyntaxNode,
state: EditorState
): boolean => {
const content = state.doc.sliceString(node.from, node.to)
return !!content.match(/%\s*\}\s*/)
}
const SEARCH_FORWARD_LIMIT = 6000
/**
* Given an opening fold Comment, find its corresponding closing Comment,
* accounting for nesting.
* */
export const findClosingFoldComment = (
node: SyntaxNode,
state: EditorState
): SyntaxNode | undefined => {
const start = node.to + 1
const upto = Math.min(start + SEARCH_FORWARD_LIMIT, state.doc.length)
const tree = ensureSyntaxTree(state, upto, HUNDRED_MS)
if (!tree) {
return
}
let closingFoldNode: SyntaxNode | undefined
let nestingLevel = 0
tree.iterate({
from: start,
to: upto,
enter: n => {
if (closingFoldNode) {
return false
}
if (n.node.type.is('Comment')) {
if (commentIsOpenFold(n.node, state)) {
nestingLevel++
} else if (commentIsCloseFold(n.node, state)) {
if (nestingLevel > 0) {
nestingLevel--
} else {
closingFoldNode = n.node
return false
}
}
}
},
})
return closingFoldNode
}
/**
* Given two Comment nodes, get the positions we want to actually fold between,
* accounting for the opening and closing brace.
*
* The resulting fold looks like `% {----}` in the editor.
*
*/
export const getFoldRange = (
startNode: SyntaxNode,
endNode: SyntaxNode,
state: EditorState
): { from: number; to: number } | null => {
const startContent = state.doc.sliceString(startNode.from, startNode.to)
const endContent = state.doc.sliceString(endNode.from, endNode.to)
const openBracePos = startContent.indexOf('{')
const closeBracePos = endContent.indexOf('}')
if (openBracePos < 0 || closeBracePos < 0) {
return null
}
return {
from: startNode.from + openBracePos + 1,
to: endNode.from + closeBracePos,
}
}
@@ -0,0 +1,92 @@
import { ensureSyntaxTree } from '@codemirror/language'
import { EditorState } from '@codemirror/state'
import { IterMode, SyntaxNode, SyntaxNodeRef, Tree } from '@lezer/common'
const HUNDRED_MS = 100
export function iterateDescendantsOf(
tree: Tree,
ancestors: (string | number)[],
spec: {
enter(node: SyntaxNodeRef): boolean | void
leave?(node: SyntaxNodeRef): void
from?: number | undefined
to?: number | undefined
mode?: IterMode | undefined
}
) {
const filteredEnter = (node: SyntaxNodeRef): boolean | void => {
if (!ancestors.some(x => node.type.is(x))) {
return false
}
return spec.enter(node)
}
tree.iterate({ ...spec, enter: filteredEnter })
}
export const previousSiblingIs = (
state: EditorState,
pos: number,
expectedName: string
): boolean | null => {
const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
if (!tree) {
return null
}
const thisNode = tree.resolve(pos)
const previousNode = thisNode?.prevSibling
return previousNode?.type.name === expectedName
}
export const nextSiblingIs = (
state: EditorState,
pos: number,
expectedName: string
): boolean | null => {
const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
if (!tree) {
return null
}
const thisNode = tree.resolve(pos)
const previousNode = thisNode?.nextSibling
return previousNode?.type.name === expectedName
}
export const getOptionalArgumentText = (
state: EditorState,
optionalArgumentNode: SyntaxNode
): string | undefined => {
const shortArgNode = optionalArgumentNode.getChild('ShortOptionalArg')
if (shortArgNode) {
const shortArgNodeText = state.doc.sliceString(
shortArgNode.from,
shortArgNode.to
)
return shortArgNodeText
}
}
export const resolveNodeAtPos = (
state: EditorState,
pos: number,
side?: -1 | 0 | 1
) => ensureSyntaxTree(state, pos, HUNDRED_MS)?.resolveInner(pos, side) ?? null
export const isUnknownCommandWithName = (
node: SyntaxNode,
command: string,
state: EditorState
): boolean => {
if (!node.type.is('UnknownCommand')) {
return false
}
const commandNameNode = node.getChild('CtrlSeq')
if (!commandNameNode) {
return false
}
const commandName = state.doc.sliceString(
commandNameNode.from,
commandNameNode.to
)
return commandName === command
}
@@ -0,0 +1,63 @@
import { CompletionContext, CompletionSource } from '@codemirror/autocomplete'
import { syntaxTree } from '@codemirror/language'
import { EditorState } from '@codemirror/state'
import { SyntaxNode } from '@lezer/common'
import { ancestorOfNodeWithType } from './ancestors'
export const ifInType = (
type: string,
source: CompletionSource
): CompletionSource => {
return (context: CompletionContext) => {
const tree = syntaxTree(context.state)
let node: SyntaxNode | null = tree.resolveInner(context.pos, -1)
while (node) {
if (node.type.is(type)) {
return source(context)
}
node = node.parent
}
return null
}
}
export function isInEmptyArgumentNodeForAutocomplete(state: EditorState) {
const main = state.selection.main
if (!main.empty) {
return false
}
const pos = main.anchor
const tree = syntaxTree(state)
if (tree.length < pos) {
return false
}
const nodeLeft = tree.resolveInner(pos, -1)
if (!nodeLeft.type.is('OpenBrace')) {
return false
}
const nodeRight = tree.resolveInner(pos, 1)
if (!nodeRight.type.is('CloseBrace')) {
return false
}
const ancestor = ancestorOfNodeWithType(
nodeLeft,
'EnvNameGroup',
'BibliographyStyleArgument',
'BibliographyArgument',
'BibKeyArgument',
'DocumentClassArgument',
'FilePathArgument',
'RefArgument',
'PackageArgument'
)
if (!ancestor) {
return false
}
return ancestor.from === nodeLeft.from && ancestor.to === nodeRight.to
}
@@ -0,0 +1,192 @@
import { ensureSyntaxTree } from '@codemirror/language'
import { EditorState } from '@codemirror/state'
import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
import { previousSiblingIs } from './common'
import { NodeIntersectsChangeFn, ProjectionItem } from './projection'
const HUNDRED_MS = 100
export class EnvironmentName extends ProjectionItem {
title = ''
}
export const enterNode = (
state: EditorState,
node: SyntaxNodeRef,
items: EnvironmentName[],
nodeIntersectsChange: NodeIntersectsChangeFn
): any => {
if (node.type.is('EnvNameGroup')) {
if (!nodeIntersectsChange(node.node)) {
return false
}
if (!node.node.prevSibling?.type.is('Begin')) {
return false
}
const openBraceNode = node.node.getChild('OpenBrace')
if (!openBraceNode) {
return false
}
const envNameNode = openBraceNode.node.nextSibling
if (!envNameNode) {
return false
}
const envNameText = state.doc.sliceString(envNameNode.from, envNameNode.to)
if (envNameText.length < 1) {
return false
}
const thisEnvironmentName = {
title: envNameText,
from: envNameNode.from,
to: envNameNode.to,
line: state.doc.lineAt(envNameNode.from).number,
}
items.push(thisEnvironmentName)
} else if (
node.type.is('NewEnvironment') ||
node.type.is('RenewEnvironment')
) {
if (!nodeIntersectsChange(node.node)) {
// This should already be in `items`
return false
}
const envNameNode = node.node.getChild('LiteralArgContent')
if (!envNameNode) {
return
}
const envNameText = state.doc.sliceString(envNameNode.from, envNameNode.to)
if (!envNameText) {
return
}
const thisEnvironmentName = {
title: envNameText,
from: envNameNode.from,
to: envNameNode.to,
line: state.doc.lineAt(envNameNode.from).number,
}
items.push(thisEnvironmentName)
}
}
export const cursorIsAtBeginEnvironment = (
state: EditorState,
pos: number
): boolean | undefined => {
const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
if (!tree) {
return
}
let thisNode = tree.resolve(pos)
if (!thisNode) {
return
}
if (
thisNode.type.is('EnvNameGroup') &&
previousSiblingIs(state, pos, 'Begin')
) {
return true
} else if (
thisNode.type.is('$Environment') ||
(thisNode.type.is('LaTeX') && pos === state.doc.length) // We're at the end of the document
) {
// We're at a malformed `\begin{`, resolve leftward
thisNode = tree.resolve(pos, -1)
if (!thisNode) {
return
}
// TODO: may need to handle various envnames
if (thisNode.type.is('OpenBrace') || thisNode.type.is('$EnvName')) {
return true
}
}
}
export const cursorIsAtEndEnvironment = (
state: EditorState,
pos: number
): boolean | undefined => {
const tree = ensureSyntaxTree(state, pos, HUNDRED_MS)
if (!tree) {
return
}
let thisNode = tree.resolve(pos)
if (!thisNode) {
return
}
if (
thisNode.type.is('EnvNameGroup') &&
previousSiblingIs(state, pos, 'End')
) {
return true
} else if (thisNode.type.is('$Environment') || thisNode.type.is('Content')) {
// We're at a malformed `\end{`, resolve leftward
thisNode = tree.resolve(pos, -1)
if (!thisNode) {
return
}
// TODO: may need to handle various envnames
if (thisNode.type.is('OpenBrace') || thisNode.type.is('EnvName')) {
return true
}
}
}
export const findDocumentEnvironment = (state: EditorState): number | null => {
const tree = ensureSyntaxTree(state, state.doc.length, HUNDRED_MS)
let position: number | null = null
tree?.iterate({
enter(nodeRef) {
if (position !== null) {
return false
}
if (nodeRef.type.is('DocumentEnvironment')) {
position = nodeRef.node.getChild('Content')?.from || null
return false
}
},
})
return position
}
/**
*
* @param node A node of type `$Environment`, `BeginEnv`, or `EndEnv`
* @param state The editor state to read the name from
* @returns The editor name or null if a name cannot be found
*/
export function getEnvironmentName(
node: SyntaxNode | null,
state: EditorState
): string | null {
if (node?.type.is('$Environment')) {
node = node.getChild('BeginEnv')
}
if (!node?.type.is('BeginEnv') && !node?.type.is('EndEnv')) {
return null
}
const nameNode = node
?.getChild('EnvNameGroup')
?.getChild('OpenBrace')?.nextSibling
if (!nameNode) {
return null
}
// the name node is a parameter in the grammar, so we have no good way to
// target the specific type
if (nameNode.type.is('CloseBrace')) {
return null
}
return state.sliceDoc(nameNode.from, nameNode.to)
}
export function getEnvironmentArguments(environmentNode: SyntaxNode) {
return environmentNode.getChild('BeginEnv')?.getChildren('TextArgument')
}
@@ -0,0 +1,27 @@
import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
import { CenteringCtrlSeq } from '../../lezer-latex/latex.terms.mjs'
export function centeringNodeForEnvironment(
node: SyntaxNodeRef
): SyntaxNode | null {
let centeringNode: SyntaxNode | null = null
const cursor = node.node.cursor()
cursor.next()
cursor.iterate(nodeRef => {
if (centeringNode) {
return false
}
if (nodeRef.from > node.to) {
return false
}
if (nodeRef.type.is(CenteringCtrlSeq)) {
centeringNode = nodeRef.node
return false
}
// don't descend into nested environments
if (nodeRef.type.is('$Environment')) {
return false
}
})
return centeringNode
}
@@ -0,0 +1,227 @@
import { EditorState } from '@codemirror/state'
import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
import { NodeIntersectsChangeFn, ProjectionItem } from './projection'
import * as tokens from '../../lezer-latex/latex.terms.mjs'
import { getEnvironmentArguments, getEnvironmentName } from './environments'
export type Outline = {
line: number
title: string
level: number
children?: Outline[]
}
/**
* A projection of a part of the file outline, typically a (sub)section heading
*/
export class FlatOutlineItem extends ProjectionItem {
level = 0
title = ''
}
export type FlatOutline = FlatOutlineItem[]
/* eslint-disable no-unused-vars */
enum NestingLevel {
Book = 1,
Part = 2,
Chapter = 3,
Section = 4,
SubSection = 5,
SubSubSection = 6,
Paragraph = 7,
SubParagraph = 8,
Frame = 9,
Invalid = -1,
}
const fallbackSectionNames: { [index: string]: NestingLevel } = {
book: NestingLevel.Book,
part: NestingLevel.Part,
chapter: NestingLevel.Part,
section: NestingLevel.Section,
subsection: NestingLevel.SubSection,
subsubsection: NestingLevel.SubSubSection,
paragraph: NestingLevel.Paragraph,
subparagraph: NestingLevel.SubParagraph,
frame: NestingLevel.Frame,
}
export const getNestingLevel = (token: number | string): NestingLevel => {
if (typeof token === 'string') {
return fallbackSectionNames[token] ?? NestingLevel.Invalid
}
switch (token) {
case tokens.Book:
return NestingLevel.Book
case tokens.Part:
return NestingLevel.Part
case tokens.Chapter:
return NestingLevel.Chapter
case tokens.Section:
return NestingLevel.Section
case tokens.SubSection:
return NestingLevel.SubSection
case tokens.SubSubSection:
return NestingLevel.SubSubSection
case tokens.Paragraph:
return NestingLevel.Paragraph
case tokens.SubParagraph:
return NestingLevel.SubParagraph
default:
return NestingLevel.Invalid
}
}
const getEntryText = (state: EditorState, node: SyntaxNodeRef): string => {
const titleParts: string[] = []
node.node.cursor().iterate(token => {
// For some reason, iterate can possibly visit sibling nodes as well as
// child nodes
if (token.from >= node.to) {
return false
}
// Hide label definitions within the sectioning command
if (token.type.is('Label')) {
return false
}
// Only add text from leaf nodes
if (token.node.firstChild) {
return true
}
titleParts.push(state.doc.sliceString(token.from, token.to))
})
return titleParts.join('')
}
/**
* Extracts FlatOutlineItem instances from the syntax tree
*/
export const enterNode = (
state: EditorState,
node: SyntaxNodeRef,
items: FlatOutlineItem[],
nodeIntersectsChange: NodeIntersectsChangeFn
): any => {
if (node.type.is('SectioningCommand')) {
const command = node.node
const parent = command.parent
if (!nodeIntersectsChange(command)) {
// This should already be in `items`
return
}
const name =
command.getChild('OptionalArgument')?.getChild('ShortOptionalArg') ??
command.getChild('SectioningArgument')?.getChild('LongArg')
if (name == null || command == null) {
return
}
// Filter out descendants of newcommand/renewcommand
for (
let ancestor: SyntaxNode | null = parent;
ancestor;
ancestor = ancestor.parent
) {
if (ancestor.type.is('NewCommand') || ancestor.type.is('RenewCommand')) {
return false
}
}
const getCommandName = () => {
const ctrlSeq = command.firstChild
if (!ctrlSeq) return ''
// Ignore the \
return state.doc.sliceString(ctrlSeq.from + 1, ctrlSeq.to)
}
const nestingLevel = parent?.type.is('$SectioningCommand')
? getNestingLevel(parent.type.id)
: getNestingLevel(getCommandName())
const thisNode = {
line: state.doc.lineAt(command.from).number,
title: getEntryText(state, name),
from: command.from,
to: command.to,
level: nestingLevel,
}
items.push(thisNode)
}
if (node.type.is('$Environment')) {
const environmentNode = node.node
if (getEnvironmentName(environmentNode, state) === 'frame') {
const beginEnv = environmentNode.getChild('BeginEnv')!
if (!nodeIntersectsChange(beginEnv)) {
// This should already be in `items`
return
}
const args = getEnvironmentArguments(environmentNode)?.map(textArg =>
textArg.getChild('LongArg')
)
if (args?.length) {
const titleNode = args[0]
const title = titleNode
? state.sliceDoc(titleNode.from, titleNode.to)
: ''
const thisNode = {
line: state.doc.lineAt(beginEnv.from).number,
title,
from: beginEnv.from,
to: beginEnv.to,
level: NestingLevel.Frame,
}
items.push(thisNode)
}
}
}
}
const flatItemToOutline = (item: {
title: string
line: number
level: number
}): Outline => {
const { title, line, level } = item
return { title, line, level }
}
export const nestOutline = (
flatOutline: { title: string; line: number; level: number }[]
): Outline[] => {
const parentStack: Outline[] = []
const outline = []
for (const item of flatOutline) {
const outlineItem = flatItemToOutline(item)
// Pop all higher-leveled potential parents from the parent stack
while (
parentStack.length &&
parentStack[parentStack.length - 1].level >= outlineItem.level
) {
parentStack.pop()
}
// Append to parent if any, and otherwise add root element
if (!parentStack.length) {
parentStack.push(outlineItem)
outline.push(outlineItem)
} else {
const parent = parentStack[parentStack.length - 1]
if (!parent.children) {
parent.children = [outlineItem]
} else {
parent.children.push(outlineItem)
}
parentStack.push(outlineItem)
}
}
return outline
}
@@ -0,0 +1,152 @@
import { ensureSyntaxTree } from '@codemirror/language'
import { EditorState, Transaction } from '@codemirror/state'
import { IterMode, SyntaxNodeRef } from '@lezer/common'
const TWENTY_MS = 20
const FIVE_HUNDRED_MS = 500
/**
* A single item in the projection
*/
export abstract class ProjectionItem {
from = 0
to = 0
line = 0
}
/* eslint-disable no-unused-vars */
export enum ProjectionStatus {
Pending,
Partial,
Complete,
}
/* eslint-enable no-unused-vars */
/*
* Result of extracting a projection from the document.
* Holds the list of ProjectionItems and the status of
* the projection
*/
export interface ProjectionResult<T extends ProjectionItem> {
items: T[]
status: ProjectionStatus
}
const intersects = (fromA: number, toA: number, fromB: number, toB: number) => {
return !(toA < fromB || fromA > toB)
}
export type NodeIntersectsChangeFn = (node: SyntaxNodeRef) => boolean
export function updatePosition<T extends ProjectionItem>(
item: T,
transaction?: Transaction
): T {
if (!transaction) {
return item
}
const { from, to } = item
const newFrom = transaction.changes.mapPos(from)
return {
...item,
from: newFrom,
to: transaction.changes.mapPos(to),
line: transaction.state.doc.lineAt(newFrom).number,
}
}
export type EnterNodeFn<T> = (
state: EditorState,
node: SyntaxNodeRef,
items: T[],
nodeIntersectsChange: NodeIntersectsChangeFn
) => any
/**
* Calculates an updated projection of an editor state. Passing a previous ProjectionResult
* will reuse the existing projection elements (though updating their position to
* point correctly into the latest EditorState), outside of the changed range.
*
* @param state The current editor state
* @param fromA The start of the modified range in the previous state.
* Ignored if `previousResult` is not provided
* @param toA The end of the modified range in the previous state.
* Ignored if `previousResult` is not provided
* @param fromB The start of the modified range in the `state`
* @param toB The end of the modified range in the `state`
* @param initialParse If this is the intial parse of the document. If that's
* the case, we allow 500ms parse time instead of 20ms
* @param enterNode A function to call when 'enter'ing a node while traversing the syntax tree,
* used to identify nodes we are interested in.
* @param transaction Optional, used to update item positions in `previousResult`
* @param previousResult A previous ProjectionResult that will be reused for
* projection elements outside of the range of [fromA; toA]
* @returns A ProjectionResult<T> pointing to locations in `state`
*/
export function getUpdatedProjection<T extends ProjectionItem>(
state: EditorState,
fromA: number,
toA: number,
fromB: number,
toB: number,
initialParse = false,
enterNode: EnterNodeFn<T>,
transaction?: Transaction,
previousResult: ProjectionResult<T> = {
items: [],
status: ProjectionStatus.Pending,
}
): ProjectionResult<T> {
// Only reuse results from a Complete parse, otherwise we may drop entries.
// We keep items that lie outside the change range, and update their positions.
const items: T[] =
previousResult.status === ProjectionStatus.Complete
? previousResult
.items!.filter(item => !intersects(item.from, item.to, fromA, toA))
.map(x => updatePosition(x, transaction))
: []
if (previousResult.status !== ProjectionStatus.Complete) {
// We have previously tried to compute the projection, but unsuccessfully,
// so we should try to parse the whole file again.
toB = state.doc.length
fromB = 0
}
const tree = ensureSyntaxTree(
state,
toB,
initialParse ? FIVE_HUNDRED_MS : TWENTY_MS
)
if (tree) {
tree.iterate({
from: fromB,
to: toB,
enter(node) {
const nodeIntersectsChange = (n: SyntaxNodeRef) => {
return intersects(n.from, n.to, fromB, toB)
}
return enterNode(state, node, items, nodeIntersectsChange)
},
mode: IterMode.IgnoreMounts | IterMode.IgnoreOverlays,
})
// We know the exact projection. Return it.
return {
status: ProjectionStatus.Complete,
items: items.sort((a, b) => a.from - b.from),
}
} else if (previousResult.status !== ProjectionStatus.Pending) {
// We don't know the latest projection, but we have an idea of a previous
// projection.
return {
status: ProjectionStatus.Partial,
items: previousResult.items,
}
} else {
// We have no previous projection, and no idea of the current projection.
// Return pending.
return {
items: [],
status: ProjectionStatus.Pending,
}
}
}
@@ -0,0 +1,86 @@
import { syntaxTree } from '@codemirror/language'
import { Line } from '@codemirror/state'
import { EditorView } from '@codemirror/view'
import { SyntaxNodeRef } from '@lezer/common'
import OError from '@overleaf/o-error'
/* A convenient wrapper around 'Normal' tokens */
export class NormalTextSpan {
public from: number
public to: number
public lineNumber: number
public text: string
public node: SyntaxNodeRef
constructor(options: {
from: number
to: number
lineNumber: number
text: string
node: SyntaxNodeRef
}) {
const { from, to, lineNumber, text, node } = options
if (
text == null ||
from == null ||
to == null ||
lineNumber == null ||
node == null
) {
throw new OError('TreeQuery: invalid NormalTextSpan').withInfo({
options,
})
}
this.from = from
this.to = to
this.text = text
this.node = node
this.lineNumber = lineNumber
}
}
const NotNormalNodeAncestors = [
'Include',
'Input',
'IncludeGraphics',
'Cite',
'LabelArgument',
'UsePackage',
'PackageArgument',
'EnvNameGroup',
'RefArgument',
]
export const getNormalTextSpansFromLine = (
view: EditorView,
line: Line
): Array<NormalTextSpan> => {
const lineNumber = line.number
const lineStart = line.from
const lineEnd = line.to
const tree = syntaxTree(view.state)
const normalTextSpans: Array<NormalTextSpan> = []
tree?.iterate({
from: lineStart,
to: lineEnd,
enter: (node: SyntaxNodeRef) => {
if (NotNormalNodeAncestors.includes(node.type.name)) {
return false
}
if (node.type.name === 'Normal') {
normalTextSpans.push(
new NormalTextSpan({
from: node.from,
to: node.to,
text: view.state.doc.sliceString(node.from, node.to),
lineNumber,
node,
})
)
return false
}
return true
},
})
return normalTextSpans
}
@@ -0,0 +1,9 @@
import * as termsModule from '../../lezer-latex/latex.terms.mjs'
export const tokenNames: Array<string> = Object.keys(termsModule)
export const Tokens: Record<string, Array<string>> = {
ctrlSeq: tokenNames.filter(name => name.match(/^(Begin|End|.*CtrlSeq)$/)),
ctrlSym: tokenNames.filter(name => name.match(/^.*CtrlSym$/)),
envName: tokenNames.filter(name => name.match(/^.*EnvName$/)),
}