ternjs / tern_for_vim

Tern plugin for Vim
MIT License
1.83k stars 100 forks source link

Use print function to avoid escaping issues. #100

Closed oblitum closed 9 years ago

oblitum commented 9 years ago

When cursor is over code like the following:

Template.Foo.events({ "change select[name='xyz']": bar });

it leads to the following stack trace because the plugin doesn't escape strings properly:

Error detected while processing function tern#LookupArgumentHints:
line   14:
Traceback (most recent call last):
Error detected while processing function tern#LookupArgumentHints:
line   14:
  File "<string>", line 1, in <module>
Error detected while processing function tern#LookupArgumentHints:
line   14:
  File "[...]/tern_for_vim/script/tern.py", line 318, in tern_lookupType
Error detected while processing function tern#LookupArgumentHints:
line   14:
    if data: tern_echoWrap(data.get("type", ""))
Error detected while processing function tern#LookupArgumentHints:
line   14:
  File "[...]/tern_for_vim/script/tern.py", line 314, in tern_echoWrap
Error detected while processing function tern#LookupArgumentHints:
line   14:
    vim.command("echo '{0}'".format(text))
Error detected while processing function tern#LookupArgumentHints:
line   14:
vim.error: Vim(echo):E121: Undefined variable: xyz
marijnh commented 9 years ago

The json.dumps calls are there precisely for escaping reasons. Are you sure you still need them when you use print? It sounds like you'd get backslash escapes before quotes that you don't want.

oblitum commented 9 years ago

@marijnh particularly I got the issue in a call that didn't have a json.dumps call. Debugging I realized the issue was due to calling vim.command("echo '{0}'".format(text)) with text like foo 'xyz' bar, which leads to echo 'foo 'xyz' bar'. Since the problem was due to having to call echo with a string literal, and the string literal wasn't built correctly, I simply avoided the problem altogether by using print which would achieve the same output effect without the need to build any literal, you just need to use the string variable directly.

marijnh commented 9 years ago

Yes, but my question is, shouldn't we remove those calls to json.dumps entirely now that we aren't going through the Python/VIM boundary anymore?

oblitum commented 9 years ago

@marijnh ah yes, I agree. I'll remove and rebase.

oblitum commented 9 years ago

@marijnh done.

marijnh commented 9 years ago

Thanks. Merged as 8e26ec9