Merge pull request #28760 from overleaf/dp-hackathon-knip

Add Knip and remove a bunch of unused code

GitOrigin-RevId: 42ab99fc65973c883d2361e0027e7181767e714e
This commit is contained in:
David
2025-10-15 08:05:57 +00:00
committed by Copybot
parent 2c7c0caf07
commit 584572bb40
43 changed files with 600 additions and 672 deletions
@@ -1,68 +1,8 @@
import { ensureSyntaxTree, syntaxTree } from '@codemirror/language'
import { syntaxTree } from '@codemirror/language'
import { EditorSelection, EditorState, SelectionRange } from '@codemirror/state'
import { SyntaxNode, Tree } from '@lezer/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 wrappedNodeOfType = (
state: EditorState,
range: SelectionRange,
@@ -1,29 +1,9 @@
import { ensureSyntaxTree } from '@codemirror/language'
import { EditorState } from '@codemirror/state'
import { IterMode, SyntaxNode, SyntaxNodeRef, Tree } from '@lezer/common'
import { SyntaxNode } 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,
@@ -38,20 +18,6 @@ export const previousSiblingIs = (
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
@@ -297,11 +297,3 @@ export function parseFigureData(
graphicsCommandArguments,
})
}
export const getBeginEnvSuffix = (state: EditorState, node: SyntaxNode) => {
const argumentNode = node
.getChild('OptionalArgument')
?.getChild('ShortOptionalArg')
return argumentNode && state.sliceDoc(argumentNode.from, argumentNode.to)
}