Technique for making a section of text uneditable/read-only?


#1

I’ve got a marker around a section of text, and I’d like to make it impossible for the user to edit it. I can’t quite figure out how to accomplish this.

TextEditor#onWillInsertText gives me an event with a cancel(), but only gives me the text that will be inserted, not the range/location.

TextBuffer#onWillChange gives me the ranges and text to be altered, but no mechanism to cancel it.

Marker only has a onDidChange and calling textEditor.undo() doesn’t seem to undo the invalidation of the marker.

Any other angles I haven’t considered yet to get this working? Seems like something that the editor should have in the core – a “read-only” type marker that prevents edits at the core level…


#2

There haven’t been any use cases so far that I know of for uneditable sections of text in a TextEditor. I don’t know of any specific support in the API for what you’re asking.


#3

Perhaps start with why you want this? It’s pretty weird for an editor to prevent edits.


#4

I apologize if I sounded rude or entitled when I used the word should… Working on packages for atom has been a great experience so far – good documentation + clean code is a winning combination.

My use case: I’m working on a REPL-in-a-text-editor and would like to make the prompts read-only, so you can’t accidentally delete the prompt. I admit this is a strange use-case… but if you’ve ever used “shell-mode” or any other mode derived from “comint-mode” in emacs, you know how powerful it can be to be inside a text editor/text buffer but also have your interactive shell/repl process.

I could see other use cases though, for creative package authors. Packages like https://atom.io/packages/tasks that are using the text buffer for novel things could use this for “header” sections, etc.

Anyway, in the meantime I can get by without it. I was just wondering if it was there and I was just missing it. Thanks for the fast replies.


#5

Alright, that makes sense. I don’t believe the texteditor in atom has been designed with this in mind. There are packages like termrk that put an interactive shell in Atom, but not inside the code editing view.

It’s also important to remember Atom supports any UI you can create with web technology. Unlike many other editors you don’t have to hack and abuse text buffers for UI. You can also add stuff to the layers above and below the code.

I’m just saying, there may be a richer alternative for what your trying to achieve. Also look at the merge conflicts package, they render UI on top of the editor based on git markers in the code.


#6

I don’t know much about how to implement what @jeffgran wants, but I can attest that something like a REPL running directly inside the editor can be useful. Having a separate REPL panel in the same window is nice, but still requires switching between different sections of the UI for writing code and testing code.

For example, Juno, the IDE for Julia, is pretty magical. Juno might go even further in integrating REPL functionality than what the OP suggests, because there isn’t even a prompt. As you’re working, you execute code snippets or the entire code file, and it’ll render commandline and graphical output right there in the editor. When you’re done looking at the output, you can click to hide it, change things, and run again. Even if you aren’t interested in using Julia, you might want to try out Juno. If I’d only heard someone describing this functionality/UI, I’m not sure I would have been too impressed, but now that I’ve tried it, I kind of wish I could do this with all my code.