Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markers are not shown in readonly models #311

Closed
ikarienator opened this issue Jan 11, 2017 · 13 comments
Closed

Markers are not shown in readonly models #311

ikarienator opened this issue Jan 11, 2017 · 13 comments
Assignees
Labels
editor-core feature-request Request for new features or functionality verified Verification succeeded
Milestone

Comments

@ikarienator
Copy link

monaco-editor npm version: 0.7.0
Browser: All
OS: All

monaco.editor.setModelMarkers does not have an effect if the module is readonly.

@alexdima
Copy link
Member

This is by design, because seeing errors on read only editors is not helpful to end users (i.e. there is no way to "fix" them since the editor is read only). You could try to workaround this using model.setEditableRange(new monaco.Range(1,1,1,1))

@alexdima alexdima self-assigned this Jan 13, 2017
@ikarienator
Copy link
Author

You have not considered the case where the source code is generated. The user would want to check the generated code with error markers shown, which, I suspect, could be one of the main use cases of a readonly model in the first place, if not the only one.

If the user would just want to have something shown to them, they can use colorize.

@alexdima alexdima reopened this Jan 16, 2017
@alexdima alexdima added feature-request Request for new features or functionality editor-core and removed question labels Jan 16, 2017
@ikarienator
Copy link
Author

Also, the workaround doesn't work because:

  1. I can still type in to the editor if the caret is at the beginning of the file, and
  2. arrow keys will not work to navigate.

I even tried to set the editable region to new monaco.Range(1, 1, 0, 0) and that does not work either.

@alexdima alexdima modified the milestone: Backlog Jun 7, 2017
@olafurpg
Copy link

olafurpg commented Jul 2, 2017

Being able to display markers (info/warning) in read-only mode would be helpful. Our application is a read-only code browser for Scala and it would be really nice to be able display markers such as compiler warnings or info messages reported by libraries (for example, http://getquill.io/ reports compile-time generated SQL queries).

@mysticatea
Copy link

I want this feature in https://mysticatea.github.io/vue-eslint-demo/

My use case is:

  • The right pane is read-only and shows auto-fixed code.
  • But some errors remain on the auto-fixed code, so I want to show the remaining errors.

@tsiq-bertram
Copy link

Are there any updates on this issue or useable workarounds?
Sharing similar concerns, we would like to display syntax errors to the users in a read-only mode

@pimterry
Copy link

I need this feature too! I'm using Monaco as a read-only view for JSON content. The user can't edit the content in this editor instance, but they're still the source of the content, and seeing validation errors is still very important. This seems like an unnecessary and fairly arbitrary restriction to me.

@pimterry
Copy link

A little background on what it'd take to change this:

Testing locally, replacing the readOnly option in both of those cases with false seems to work nicely for me, and I immediately see the behaviour I'm expecting. I'm sure there'll be other places that need changing too on closer inspection, but overall it looks like it should be very easy to remove this restriction.

My one concern is that VS Code (and other users) likely depend on this behaviour as their way of hiding validation errors in cases where users really don't want to see them. We could solve that with a new Monaco option (readOnlyValidation: true, or perhaps forceValidation) to force validation on read only content, so that this new behaviour is opt-in.

What do you think?

(In the meantime, I'd love other suggestions for workarounds! This is a major problem for my application, as showing nice validation errors is one of the main reasons I'm using Monaco)

@tsiq-bertram
Copy link

Current workaround that I'm using is to reset the model in custom read-only mode. Below example is specific to ReactJS, but would work similarly in any other framework. Drawback is that the cursor still moves when typing (can probably be prevented by storing and resetting cursor position as well), and you'll have to provide a custom "read-only" message elsewhere in your application.

const editor = monaco.editor.create(...)
const model = editor.getModel()
model.onDidChangeContent(() => {
      if (this.props.readOnly && model.getValue() !== this.props.value) {
        model.setValue(this.props.value)
      }
      this.props.onChange(model.getValue(), model)
    })

@pimterry
Copy link

Nice, thanks @tsiq-bertram! I've tested that out but, as you mentioned, it does have some awkward side effects. I think I've found a side-effect free workaround:

function enableMarkers(model) {
    if (!model) return;

    [
        ['getLineDecorations', 2],
        ['getLinesDecorations', 3],
        ['getDecorationsInRange', 2],
        ['getOverviewRulerDecorations', 1],
        ['getAllDecorations', 1],
    ].forEach(([functionName, maxArgs]) => {
        const originalMethod = model[functionName];
        model[functionName] = function() {
            return originalMethod.apply(this, Array.from(arguments).slice(0, maxArgs));
        };
    });
}

That code takes every method on the model for the line decorations which takes a filterOutValidation parameter, and wraps it in a function that drops that parameter (i.e. never disables validation).

To make it work, you just need to call this function on the model any time it changes. I'm doing that with:

enableMarkers(editor.getModel());
editor.onDidChangeModel(() => enableMarkers(editor.getModel()));

Seems to work perfectly. All other readonly behaviour works as before, but now decorations appear as well.

This is decidedly fragile in the face of future changes to the model of course, but at a glance I wouldn't expect these methods to be changing drastically any time soon, so that's acceptable for my case.

@ikarienator
Copy link
Author

The workaround we got is this:

  onDidChangeContentGuard = 0;
  ...
  onDidChangeContent = e => {
    let model = this._editor.getModel();
    if (this.props.readOnly) {
      if (this.onDidChangeContentGuard === 0) {
        this.onDidChangeContentGuard++;
        try {
          const viewStates = this._editor.saveViewState();
          model.setValue(this.props.binding.value);
          this._editor.restoreViewState(viewStates);
        } finally {
          this.onDidChangeContentGuard--;
        }
      }
      return;
    }
    ...
  };

I think this is less intrusive.

@alexdima alexdima modified the milestones: Backlog, On Deck Mar 1, 2019
@alexdima alexdima modified the milestones: On Deck, Backlog Dec 16, 2019
@alexdima
Copy link
Member

microsoft/vscode@c7cc30c adds a new editor option, renderValidationDecorations that can be set to on and in this case, the editor will render squiggles even if it is read only.

@alexdima alexdima modified the milestones: Backlog, January 2020 Jan 24, 2020
@alexdima
Copy link
Member

This will be verified via microsoft/vscode#89057

@alexdima alexdima added the verified Verification succeeded label Jan 28, 2020
@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
editor-core feature-request Request for new features or functionality verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants