microsoft / microsoft/monaco-editor

[Feature Request] Export `PieceTreeTextBufferFactory` and friends

Open
#2,974 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

editor-core feature-request
Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Context
  • This issue is not a bug report. (please use a different template for reporting a bug)
  • This issue is not a duplicate of an existing issue. (please use the search to find existing issues)
Description

I need to opt out of line ending normalization (https://github.com/microsoft/vscode/issues/127) and with the current API I can get very close. Turns out the entire functionality is already there (?), it just isn't used / exposed. I thought this would involve refactoring the text buffer but it's literally there since 2018? (IDC about VS Code integration and exposing this in the UI)

If I create a text model then model._buffer._pieceTree._EOLNormalized === true. But setValue will create a new buffer, so I can't just flip this to false.

  1. setValue https://github.com/microsoft/vscode/blob/54dec24a4d1e67dc6cb7fae0e6119dc84e0fab92/src/vs/editor/common/model/textModel.ts#L464-L473
  2. eventually creates a PieceTreeTextBufferFactory https://github.com/microsoft/vscode/blob/54dec24a4d1e67dc6cb7fae0e6119dc84e0fab92/src/vs/editor/common/model/textModel.ts#L57-L61
  3. which does the normalization https://github.com/microsoft/vscode/blob/54dec24a4d1e67dc6cb7fae0e6119dc84e0fab92/src/vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder.ts#L46-L56

If I'm not mistaken all I need is a way to create a PieceTreeTextBufferFactory and then I can use model._setValueFromTextBuffer. And later I can use model._buffer._pieceTree.getValueInRange() which gives me the literal value without normalization? https://github.com/microsoft/vscode/blob/8dcfb37475324c4da68475b073b22f2ccdb4fe5e/src/vs/editor/common/model/pieceTreeTextBuffer/pieceTreeBase.ts#L458-L481
What I mean by "I get very close" is that I can access model._buffer._pieceTree.constructor, so I'm just one API off 😄 . I can do this

let model = monaco.editor.createModel('');
const StringBuffer = model._buffer._pieceTree._buffers[0].constructor;
const PieceTreeTextBuffer = model._buffer._pieceTree.constructor;

But I don't know how to create a StringBuffer (chunks), because the PieceTreeTextBufferFactory does that and it seems a little involved.

So would it be possible to expose some of these APIs and make them public? Or is it still not as simple as it appears? This would allow more control over EOL in monaco itself. All that would be left to get full control would be https://github.com/microsoft/monaco-editor/issues/953, e.g. sth. like a TextModel.onWillChangeContent that can be used to prevent a change or update it (e.g. to replace EOL in the IModelContentChange.text).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/vs/editor/common/model/textModel.ts at setValue and the PieceTreeTextBufferFactory references, then read pieceTreeTextBuffer/pieceTreeTextBufferBuilder.ts and pieceTreeBase.ts. Determine which factory and buffer APIs can be public without relying on private model fields. Done means the required APIs are intentionally exposed and support creating a buffer without line-ending normalization, with relevant behavior verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.