All files / src codeEditorProvider.ts

0% Statements 0/170
100% Branches 1/1
100% Functions 1/1
0% Lines 0/170

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import type { CodeBlockInfo } from './codeDetector'
import { Buffer } from 'node:buffer'
import jsesc from 'jsesc-es'
import * as vscode from 'vscode'
import { generateTempFileName } from './languageUtils'
import { logger } from './logger'
import { ensureTempDirectoryExists, getTempDirectoryUri } from './tempUtils'
 
export class CodeEditorProvider {
  private context: vscode.ExtensionContext
  private activeEditors: Map<string, {
    tempDocument: vscode.TextDocument
    originalDocument: vscode.TextDocument
    originalEditor: vscode.TextEditor
    codeInfo: CodeBlockInfo
    disposables: vscode.Disposable[]
  }> = new Map()
 
  constructor(context: vscode.ExtensionContext) {
    this.context = context
  }
 
  async openCodeEditor(
    codeInfo: CodeBlockInfo,
    originalDocument: vscode.TextDocument,
    originalEditor: vscode.TextEditor,
  ): Promise<void> {
    try {
      // 创建临时文件路径
      const tmpDirUri = getTempDirectoryUri()
      const timestamp = Date.now()
      const tempFileName = generateTempFileName(codeInfo.fieldName, codeInfo.language, timestamp)
      const tempFileUri = vscode.Uri.joinPath(tmpDirUri, tempFileName)
 
      // 确保临时目录存在
      await ensureTempDirectoryExists()
 
      // 创建临时文件内容
      const codeContent = codeInfo.code || ''
      await vscode.workspace.fs.writeFile(tempFileUri, Buffer.from(codeContent, 'utf-8'))
 
      // 打开临时文件
      const tempDocument = await vscode.workspace.openTextDocument(tempFileUri)
 
      // 打开临时编辑器
      await vscode.window.showTextDocument(tempDocument, {
        viewColumn: vscode.ViewColumn.Beside,
        preview: false,
      })
 
      // 设置编辑器标题
      const editorId = tempDocument.uri.toString()
 
      // 设置语言模式
      await vscode.languages.setTextDocumentLanguage(tempDocument, codeInfo.language)
 
      // 创建状态栏项
      const statusBarItem = vscode.window.createStatusBarItem(
        vscode.StatusBarAlignment.Left,
        100,
      )
      statusBarItem.text = `$(edit) Editing: ${codeInfo.fieldName} (${codeInfo.language})`
      statusBarItem.tooltip = `${codeInfo.language} code is being edited. Save or close to sync changes.`
      statusBarItem.show()
 
      // 监听文档关闭
      const closeListener = vscode.workspace.onDidCloseTextDocument((document: vscode.TextDocument) => {
        if (document === tempDocument) {
          // 在关闭时同步更改
          this.syncChangesToOriginal(tempDocument, originalDocument, originalEditor, codeInfo)
          this.cleanupEditor(editorId)
        }
      })
 
      // 监听编辑器关闭
      const editorCloseListener = vscode.window.onDidChangeVisibleTextEditors((editors: readonly vscode.TextEditor[]) => {
        const tempEditorStillOpen = editors.some((editor: vscode.TextEditor) => editor.document === tempDocument)
        if (!tempEditorStillOpen) {
          // 在关闭时同步更改
          this.syncChangesToOriginal(tempDocument, originalDocument, originalEditor, codeInfo)
          this.cleanupEditor(editorId)
        }
      })
 
      // 监听保存事件,在保存时同步到原始文件
      const saveListener = vscode.workspace.onWillSaveTextDocument((event: vscode.TextDocumentWillSaveEvent) => {
        if (event.document === tempDocument) {
          // 在保存时同步更改到原始JSON文件
          this.syncChangesToOriginal(tempDocument, originalDocument, originalEditor, codeInfo)
          // 显示同步成功消息
          logger.info(`Changes synced to the original JSON file for field: ${codeInfo.fieldName}`)
        }
      })
 
      // 存储编辑器信息
      this.activeEditors.set(editorId, {
        tempDocument,
        originalDocument,
        originalEditor,
        codeInfo,
        disposables: [closeListener, editorCloseListener, saveListener, statusBarItem],
      })
 
      // 显示成功消息
      logger.info(`Opened code editor for field: ${codeInfo.fieldName}, language: ${codeInfo.language}`)
    }
    catch (error) {
      logger.error(`Failed to open JavaScript editor: ${error}`)
    }
  }
 
  private syncChangesToOriginal(
    tempDocument: vscode.TextDocument,
    originalDocument: vscode.TextDocument,
    originalEditor: vscode.TextEditor,
    codeInfo: CodeBlockInfo,
  ): void {
    try {
      const newCode = tempDocument.getText()
      const originalCode = codeInfo.code || ''
 
      // 检查内容是否真的发生了变化
      if (newCode === originalCode) {
        // 内容没有变化,不需要同步
        return
      }
 
      const escapedCode = this.escapeForJson(newCode)
 
      // 计算原始文档中需要替换的范围
      const originalText = originalDocument.getText()
      const beforeQuote = originalText.lastIndexOf('"', codeInfo.start - 1)
      const afterQuote = originalText.indexOf('"', codeInfo.end)
 
      if (beforeQuote !== -1 && afterQuote !== -1) {
        const replaceRange = new vscode.Range(
          originalDocument.positionAt(beforeQuote + 1),
          originalDocument.positionAt(afterQuote),
        )
 
        // 应用编辑
        const edit = new vscode.WorkspaceEdit()
        edit.replace(originalDocument.uri, replaceRange, escapedCode)
 
        vscode.workspace.applyEdit(edit).then((success: boolean) => {
          if (success) {
            // 同步成功后,更新存储的codeInfo以反映新的位置
            this.updateCodeInfoAfterSync(tempDocument, originalDocument, codeInfo, escapedCode)
          }
          else {
            logger.error('Failed to sync changes to original document')
          }
        })
      }
    }
    catch (error) {
      logger.error(`Failed to sync changes: ${error}`)
    }
  }
 
  private updateCodeInfoAfterSync(
    tempDocument: vscode.TextDocument,
    originalDocument: vscode.TextDocument,
    oldCodeInfo: CodeBlockInfo,
    newEscapedCode: string,
  ): void {
    try {
      // 重新计算代码在原始文档中的位置
      const originalText = originalDocument.getText()
      const fieldPattern = `"${oldCodeInfo.fieldName}"\\s*:\\s*"`
      const fieldRegex = new RegExp(fieldPattern)
      const fieldMatch = originalText.match(fieldRegex)
 
      if (fieldMatch) {
        const fieldIndex = originalText.indexOf(fieldMatch[0])
        const codeStartIndex = fieldIndex + fieldMatch[0].length
        const codeEndIndex = codeStartIndex + newEscapedCode.length
 
        // 更新存储的codeInfo
        const editorId = tempDocument.uri.toString()
        const editorInfo = this.activeEditors.get(editorId)
        if (editorInfo) {
          editorInfo.codeInfo.start = codeStartIndex
          editorInfo.codeInfo.end = codeEndIndex
          editorInfo.codeInfo.range = new vscode.Range(
            originalDocument.positionAt(codeStartIndex),
            originalDocument.positionAt(codeEndIndex),
          )
          editorInfo.codeInfo.code = tempDocument.getText()
        }
      }
    }
    catch (error) {
      console.error('Failed to update codeInfo after sync:', error)
    }
  }
 
  private escapeForJson(str: string): string {
    return jsesc(str, {
      json: true, // 确保输出是有效的JSON
      wrap: false, // 不包含外层引号,因为我们只替换引号内的内容
      es6: false, // 不使用ES6语法
      minimal: false, // 不使用最小转义
    })
  }
 
  private cleanupEditor(editorId: string): void {
    const editorInfo = this.activeEditors.get(editorId)
    if (editorInfo) {
      // 清理所有监听器
      editorInfo.disposables.forEach(disposable => disposable.dispose())
 
      // 删除临时文件
      if (editorInfo.tempDocument.uri.scheme === 'file') {
        vscode.workspace.fs.delete(editorInfo.tempDocument.uri, { useTrash: false }).then(
          () => {},
          (error: any) => {
            console.error('Failed to delete temp file:', error)
          },
        )
      }
 
      // 从映射中移除
      this.activeEditors.delete(editorId)
 
      logger.info('Code editor closed. Changes have been synced.')
    }
  }
 
  dispose(): void {
    // 清理所有活动编辑器
    for (const [editorId] of this.activeEditors) {
      this.cleanupEditor(editorId)
    }
  }
}