Sunday 30 May 2021

VS Code 側

午前中は TypeScript のお勉強をしてました。だって、

  declare const Buffer: any;
  const hasBuffer = (typeof Buffer !== 'undefined');
  export class VSBuffer {
      static alloc(byteLength: number): VSBuffer {
          if (hasBuffer) {
              return new VSBuffer(Buffer.allocUnsafe(byteLength));
          } else {
              return new VSBuffer(new Uint8Array(byteLength));
          }
      }

いきなり、これですよ。なんだよ、このコード。any で受けて、hasBuffer ですか? record型は面白いが微妙。

  % ls ~/src/public/vscode/src/vs/editor/common
  commands/       core/         editorCommon.ts    model.ts       services/       view/
  config/        diff/         editorContextKeys.ts modes/        standalone/      viewLayout/
  controller/      editorAction.ts    model/        modes.ts       standaloneStrings.ts viewModel/

というわけで、正直なMVCでよろしい。で、

  % more ~/src/public/vscode/src/vs/editor/common/model/textModel.ts

が Model というところまではすぐにわかったんですが...

  key input から model 変更の道筋がわからない

VSCode + Chrome extension で debug していくわけなんですが、さっぱりわかりません。なのだが、学生が getRange で捕まえられるっての発見。
僕がわからないところを見つけてくるのは偉すぎる。

なのだが、

  stack trace をコピペできないのでスクショ

なんだよそれ。でわかったのは、

  # In vs/workbench/browser/parts/editor/textResourceEditor.ts
  60:
  61:  // Remember view settings if input changes
  62:  this.saveTextResourceEditorViewState(this.input);
  63:
  64:  // Set input and resolve
  65:  await super.setInput(input, options, context, token);
  66:  const resolvedModel = await input.resolve();

await で resolve しているわけね。setInput ではわからん。

結局、
  # editStack.ts
  public pushEditOperation()
で editor command を undo 用に取っておいて、

  # vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBuffer.ts
   public applyEdits(rawOperations: ValidAnnotatedEditOperation[], recordTrimAutoWhitespace: boolean, computeUndoEdits: boolean): ApplyEditsResult {

で処理するってところまで見れました。この辺のcomandにhookがあるので、それでいろいろできるらしい。

行単位で変更前と後を持っている贅沢な作りだな。

No comments: