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

How can I call lint() manually for standard linter? #1511

Open
cimfalab opened this issue Jul 18, 2017 · 4 comments
Open

How can I call lint() manually for standard linter? #1511

cimfalab opened this issue Jul 18, 2017 · 4 comments

Comments

@cimfalab
Copy link

I have a standard linter like:

    provideLinter() {
        return {
            name: 'xxx',
            grammarScopes: this.grammers,
            scope: 'file',
            lintsOnChange: false,
            lint: async (textEditor) => {
                return this.inspect(textEditor);
            }
        }
    },

My linter works only on save intentionally.
So new lint result by changed setting can be applied only when a user saves although he changed a setting.

So I want to execute lint() for all open text editors directly after setting change like:

        let view = atom.views.getView(editor);
        return atom.commands.dispatch(view, 'linter:lint');

But it does nothing. lint() is not called.

How can I call lint() manually for standard linter?
Please let me know.

@steelbrain
Copy link
Owner

I think you should not worry about having to apply changes of settings live as they'll be like that in only your package and not the other bazillion linter packages out there. The new settings will be applied as soon as the user changes text (for lintsOnChange providers) or saves the file (for standard linters).

But if you really want to do that, the command invocation should work flawlessly. If it's not working for you then I'll be able to investigate if you can put a piece of code out there that repros the behavior.

@cimfalab
Copy link
Author

Thank you for your reply.
Although I don't need to apply changes of settings live as you comment, I want to try :)

I would appreciate if you investigate the code.
When I change a setting, listener is called and command is invoked in runLinter().
But lint() provided in provideLinter() is not called.

'use babel';

import { CompositeDisposable, Emitter, Task } from 'atom';

module.exports = {
    activate() {
        this.emitter = new Emitter();
        this.subscriptions = new CompositeDisposable();

        this.grammers = ['source.js', 'source.js.jsx'];

        this.subscriptions.add(this.emitter);

        this.subscriptions.add(atom.config.observe(`atom-deepscan`, (value) => {
            // Reinspect any open text documents
            let documents = [];
            for (let editor of atom.workspace.getTextEditors()) {
                if (this.grammers.includes(editor.getGrammar().scopeName)) {
                    documents.push(editor);
                }
            }
            documents.forEach(this.runLinter);
        }));

        this.subscriptions.add(atom.workspace.observeTextEditors((editor) => {
        }));
    },

    deactivate() {
        this.subscriptions.dispose();
    },

    provideLinter() {
        return {
            name: 'Test Linter',
            grammarScopes: this.grammers,
            scope: 'file',
            lintsOnChange: false,
            lint: async (textEditor) => {
                console.log('lint');
                return this.inspect(textEditor);
            }
        }
    },

    runLinter(editor) {
        let view = atom.views.getView(editor);
        console.log('runLinter', view);
        return atom.commands.dispatch(view, 'linter:lint');
    },

    async inspect(textEditor) {
        const text = textEditor.getText();
        if (text.length === 0) {
            return [];
        }

        const filePath = textEditor.getPath();

        return [{
            severity: 'warning',
            location: {
                file: filePath,
                position: [[1, 1], [1, 3]]
            },
            excerpt: 'test'
        }]
    }
}

package.json

  "engines": {
    "atom": ">=1.13.0 <2.0.0"
  },
  "configSchema": {
    "server": {
      "title": "Server",
      "description": "Set an url",
      "type": "string",
      "default": "https://abc.com"
    }
  },
  "scripts": {
    "test": "apm test"
  },
  "dependencies": {
    "atom-babel6-transpiler": "^1.1.1",
    "babel-preset-node5": "^12.0.0",
    "atom-linter": "^10.0.0",
    "crypto-random-string": "^1.0.0",
    "request": "^2.79.0"
  },
  "devDependencies": {
  },
  "package-deps": [
    "linter:2.0.0"
  ],
  "providedServices": {
    "linter": {
      "versions": {
        "2.0.0": "provideLinter"
      }
    }
  },
  "atomTranspilers": [
    {
      "glob": "{src,spec}/*.js",
      "transpiler": "atom-babel6-transpiler",
      "options": {
        "babel": {
          "presets": [
            "node5"
          ],
          "sourceMaps": "inline",
          "babelrc": false
        },
        "cacheKeyFiles": [
          "package.json"
        ]
      }
    }
  ]

@db-developer
Copy link

db-developer commented Feb 21, 2022

I can confirm that calling atom.commands.dispatch( view, "linter:lint" ); does not result in a re-linting of the specified views.
Example: click here - line 129

@db-developer
Copy link

db-developer commented Feb 21, 2022

Well ... when running the same lines from the developer console, the linter gets triggered.
So, it seems, I do run the trigger at a wrong point. Obviously 'inside here' is the wrong place.

activate() {
    this.subscriptions.add( atom.config.observe( _STRINGS.BASECONFIG,    ( presets ) => {
      updateBaseCfg( presets, this.grammarScopes ); // <= inside here
    }));
}

At which other point, could I trigger a re-lint after a configuration change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants