MCPcopy
hub / github.com/imcuttle/mometa / insertNodeMiddleware

Function insertNodeMiddleware

packages/fs-handler/src/common/insert-node.ts:5–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import { getAddMaterialOps } from './add-material'
4
5export default function insertNodeMiddleware(): Middleware {
6 return async (data, ctx, next) => {
7 if (data.type === OpType.INSERT_NODE) {
8 const toPos = data.preload.to
9 if (!toPos) {
10 return next()
11 }
12
13 const { newText = '', wrap, material } = data.preload.data || {}
14 const line = ctx.lineContents.locateLineByPos(toPos.line)
15 let lineChars = line.toChars()
16
17 const insertContent = async () => {
18 if (newText) {
19 lineChars.insert(toPos.column, newText)
20 } else if (material) {
21 const ops = await getAddMaterialOps(ctx.lineContents, toPos, material)
22 await ctx.runLoop(ops.insertDeps as any, ctx)
23 if (ops.insertCode) {
24 // @ts-ignore
25 lineChars.insert(toPos.column, ops.insertCode.preload.data?.newText)
26 }
27 }
28 }
29
30 if (wrap) {
31 const { startStr = '', endStr = '', anotherTo } = wrap
32 const anotherLine = ctx.lineContents.locateLineByPos(anotherTo.line)
33 const anotherChars = anotherLine === line ? lineChars : anotherLine.toChars()
34
35 const flag = comparePoint(toPos, anotherTo)
36 if (flag > 0) {
37 lineChars.insert(toPos.column, endStr)
38 await insertContent()
39 anotherChars.insert(anotherTo.column, startStr)
40 } else {
41 anotherChars.insert(anotherTo.column, endStr)
42 await insertContent()
43 lineChars.insert(toPos.column, startStr)
44 }
45 anotherLine.setContent(anotherChars.toString())
46 } else {
47 await insertContent()
48 }
49
50 line.setContent(lineChars.toString())
51 // line.setContent(line.slice(0, toPos.column).content + newText + line.slice(toPos.column ?? Infinity).content)
52 }
53 return next()
54 }
55}

Callers 1

commonMiddlewaresFunction · 0.85

Calls 7

comparePointFunction · 0.90
insertContentFunction · 0.85
locateLineByPosMethod · 0.80
toCharsMethod · 0.80
insertMethod · 0.80
setContentMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected