pragmagic / vscode-nim

An extension for VS Code which provides support for the Nim language.
Other
237 stars 37 forks source link

Autocompletion via nimsuggest does not work on Windows #50

Closed rhencke closed 7 years ago

rhencke commented 7 years ago

Currently, when you attempt to invoke autocompletion in Windows, no results from nimsuggest appear.

I believe I have traced this down to the current implementation of sexp, and how replacements are handled for strings.

In the current revision of vscode-nim, sexp's toString is defined as:

export function toString(sexp: SExp) {
    switch (sexp.kind) {
        case 'cons':
            return '(' + toString(sexp.car) + ' . ' + toString(sexp.cdr) + ')';
        case 'list':
            let stringRepr = '(';
            sexp.elements.forEach(element => {
                stringRepr += toString(element) + ' ';
            });
            return stringRepr.substr(0, stringRepr.length - 1) + ')';
        case 'number':
            return sexp.n.toString();
        case 'ident':
            return sexp.ident;
        case 'string':
            return '\"' + sexp.str.replace('\n', '\\n').replace('\r', '\\r').replace('\\', '\\\\').replace('\"', '\\\"') + '\"';
        case 'nil':
            return 'nil';
    }
}

In one code path, a string value of C:\Users\rhencke\AppData\Local\Temp\vscodenimdirty.nim is passed.

This is then escaped as C:\\Users\rhencke\AppData\Local\Temp\vscodenimdirty.nim, because the replace function only will replace the first occurrence of a string, when given a string as the search parameter.

If the string case is modified to:

return '\"' + sexp.str.replace(/\\/g, '\\\\'). replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\"/g, '\\\"') + '\"';

the string value is escaped to C:\\Users\\rhencke\\AppData\\Local\\Temp\\vscodenimdirty.nim, and autocompletion now works.

RSDuck commented 7 years ago

that's interesting, I actually wrote this piece of code on Windows and it works for me, but what you mention is definetely a bug.

I fixed the bug you've described(I used JSON.stringify which should cover every case, including unicode escape sequences etc.) in my fork. Can you please try, if it fixes the bug for you: https://github.com/RSDuck/vscode-nim. For the case, you don't have the needed build tools installed, I attached a compiled version of the extension. You have to remove the .zip extension and install inside of VSCode(https://code.visualstudio.com/docs/editor/extension-gallery#_install-from-a-vsix). Make sure you deinstalled the downloaded version of the extension before.

nim-0.5.22.vsix.zip

rhencke commented 7 years ago

Thank you! I uninstalled the old version, and installed the one you provided (via the zip file), and can confirm that it fixes it. Everything comes up from nimsuggest on the first try now.

RSDuck commented 7 years ago

Ok, I just made a pull request. It shouldn't take too long until it gets merged and a new release gets pushed.