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

Use language from editor in highlight.js #17

Closed
ror3d opened this issue Mar 27, 2019 · 7 comments
Closed

Use language from editor in highlight.js #17

ror3d opened this issue Mar 27, 2019 · 7 comments

Comments

@ror3d
Copy link

ror3d commented Mar 27, 2019

Right now it's left to highlight.js to decide what language the document is using. This might not be best, I have found that it sometimes doesn't work as intended, so I think it would be best to get the language defined in the editor to tell highlight.js which one to use.

I implemented something similar to this (in the already compiled js) which looks something similar to this:

function getSourceCodeLanguage() { // Copied getSourceCode and changed some things so it gets the language instead of the file contents
    var sender = vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri.fsPath === commandArgs.fsPath ?
        "ACTIVE TEXT EDITOR" :
        "FILE EXPLORER";
    let result = "THIS CAN'T HAPPEN";
    switch (sender) {
        case "ACTIVE TEXT EDITOR":
            if (vscode.window.activeTextEditor) {
                if (selection && !(selection.isEmpty || selection.isSingleLine)) {
                    result = vscode.window.activeTextEditor.document.languageId;
                }
                else {
                    result = vscode.window.activeTextEditor.document.languageId;
                }
            }
            break;
        case "FILE EXPLORER":
            try {
                let td = vscode.workspace.openTextDocument(commandArgs.fsPath);
                result = td.languageId;
            }
            catch (error) {
                throw new Error(`Cannot access ${commandArgs.fsPath}.\n${error.Message}`);
            }
            break;
    }
    return result;
}

And later

    let renderedCode = hljs.highlight(getSourceCodeLanguage(), getSourceCode()).value;

This seemed to fix my issue and now the document is properly highlighted.

I suppose there should be a check to make sure that highlight.js knows the languageId reported by vscode.

@PeterWone
Copy link
Collaborator

Good thinking. When can I expect the PR?

@ror3d
Copy link
Author

ror3d commented Mar 28, 2019

Hm I have never used Typescript so I'm not sure I can make this work properly without spending too much time in it...

@PeterWone
Copy link
Collaborator

I can do it, but if I do it won't be attributed to the right author, which seems a bit counter to the spirit of open source.

@ror3d
Copy link
Author

ror3d commented Mar 28, 2019

I really don't mind though, I contribute only so things are better, not really to get credit for it, but thanks for your concern!

@PeterWone
Copy link
Collaborator

I folded it into getSourceCode and returned string[] to pass two result values. This avoids double loading when it's not the active editor.

We're assuming perfect correlation between vscode and highlightjs names for languages, but a cursory inspection suggests this is reasonable. At any rate it works for SQL, C#, TS, JS and MD. And PAS if you install the Pascal extension.

@ror3d
Copy link
Author

ror3d commented Mar 30, 2019

It worked for me for MIPS Assembly so I'd say it's probably right.

If not, maybe there's a way to tell highlight to use the provided name or something else if that's not good? I saw that you could give it "*" as a language parameter so that it looked through all, but I'm not sure if that can be used together with specifying a preferred language.

@PeterWone
Copy link
Collaborator

Let's close this. If anyone runs into an exception I'll create a mapping list but until then lets not fix imaginary problems.

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

No branches or pull requests

2 participants