Vim Javascript autocompletion picks comments as suggestions - javascript

I have the following line in my vimrc to enable Javascript completion:
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
The problem is that if I'm working on a JS file that contains a lot of comments, for example:
// draws the map and the pieces depending on the state of the game
Vim picks words from those comments, such as draws, map, etc.; and considers them as acceptable code suggestions, which they are clearly not. How can I filter them out?

Vim has a number of completion mechanism suited for different needs: keyword completion, file name completion… and omni completion, the kind of completion most suited to programming.
Omni completion is usually initiated by pressing <C-x><C-o> and will certainly never pick a suggestion from the comments in your file. Are you sure you are using omni completion and not something else? <C-n> or <C-p>, maybe? See :h ins-completion for the full list.
Also, that line is totally useless. Supposing you have filetype plugin indent on in your ~/.vimrc, it's completely unnecessary to tell Vim to use JS completion in JS file.

JS autocompletion requires dynamic type inference, which is difficult for non-IDE text editors.(JS has no static class, you know!) Vim probably just implement a fuzzy matching algorithm to make completion. (I just guess, because I don't use vim personally). I think Vim also takes comments into consideration because it probably does not interpret js!
You can try the new Ternjs plugin for Vim. You need node.JS. Make sure your Vim has Python support.
Online demo is on http://ternjs.net/
Generally, javascript autocompletion is difficult to accomplish due to the dynamic nature of that language. Ternjs makes type inference like javascript engine, and thus makes autocompletion better.
(Personally I have tried tern in SublimeText. Great Experience except initial loading and no builtin function such as document.body.appendChild)

Related

Syntax folding in JavaScript files does not work in Vim

I've started using Vim 7.4 on Ubuntu and am very pleased with it but there is just one thing driving me crazy: code folding doesn't work (at least for JavaScript)!
The syntax is automatically set to JavaScript when a js file is opened and syntax highlight works so I don't get it. The foldmethod is initially set to "manual" and setting it to "syntax" doesn't make a difference, which puzzles me. I did add let javaScript_fold=1 to my .vimrc file.
Any clue? I'd be very grateful. Thanks!
It's tough to say the exact cause of this issue, but if you don't have a javascript.vim file you probably should. I suggest starting with this enhanced javascript syntax config. It is likely to fix your javascript folding issue, and much more.
If you just want to focus on the folding issue you might try creating your own javascript.vim file in ~/.vim/syntax/javascript.vim that contains code along the lines of what I have given below. You may want to adjust the fold level to your liking (0 is completely folded). However, this simple version will not play well with comments containing curly bracket characters, which is where you will want to go with a more robust javascript.vim like the one I have linked.
syntax region foldBraces start=/{/ end=/}/ transparent fold keepend extend
setlocal foldmethod=syntax
setlocal foldlevel=0
I should add that both myself and the other responder are suggesting that you need a javascript.vim, and in fact by some of the same contributors. However, the one I am suggesting was last updated in December of 2015 as opposed to 2009.
I don't know why your solution isn't working, but a possible solution is to use a user-created vimscript available at http://www.vim.org/scripts/script.php?script_id=1491
Just had this same issue answered on Vim Stack Exchange, and the answer is that if you do use the stock syntax/javascript.vim file, then you have to set
vv
let g:javaScript_fold = 1
^^
The difference between the command in the question and here is the g: part (highlighted above). I'm new to Vim scripting, but I believe the difference is that let javaScript_fold=1 sets a script-local variable, making it confined to your .vimrc file, and the example above makes it global (which seems to be confirmed by this Stackoverflow thread). See more on this at section 41.2 Variables in :help usr_41.txt and :help internal-variables.
This Reddit thread was also enlightening; it's not JavaScript-related but the folding seems to be useful for JS files as well.

Node.js function metadata for code completion

So, the core of my problem is that I might not know what the thing I want is called. I think it's called "metadata", but Google doesn't turn up anything.
Anyway when you type a function's name in WebStorm (though I can not imagine this being unique to it) it will sometimes display a tooltip, telling you what the function does and what parameters it takes- this really cuts down the time needed to look stuff up. So, I want to bundle those with the functions I write. How do I do that, or at least what is it properly called?
There isn't one term for it, unfortunately. Microsoft's term is "IntelliSense." Various other terms are listed in this answer on programmers.stackexchange.com:
Auto Code Completion
Code Completion
Code Hinting
Code Assist
I believe the term they use at JetBrains (the people behind WebStorm and IntelliJ and such) is "Code Completion," based on the WebStorm help.
More about WebStorm code completion on their blog. It looks like they'll work from the source code, using JSDoc if they find it.

Renaming variables in JavaScript

I've been stuck with the unpleasant task of "unminifying" a minified JavaScript code file. Using JSBeautifier, the resulting file is about 6000 lines long.
Ordinarily, the variable and parameter names would be permanently lost, but in this case, I have an obsolete version of the original file that the minified JavaScript code file was generated from. This obsolete version of the original file contains most of the code comments and variable names, but absolutely cannot be used in place of the current version.
I would like to know if there is some way of renaming all instances of a particular parameter or variable in JavaScript. Since minification reduces the names to a single character, find-and-replace is impossible.
Is there some tool out there, which I can tell, in this file, the parameter a to function foo should be clientName and have it semantically rename all instances of that parameter to clientName?
Unfortunately, I work for a large organization with an approved list of software and I am stuck with Visual Studio 2010 for the forseeable future (no VS 2012).
Update: #Kos, we don't use Git, but we do use source control. The problem is that a developer who doesn't work for my organization anymore once made changes to the file, minified it, and only checked in the minified version to source control, so his changes to the original have been lost.
I'm a year late for this answer, but I had a similar problem to yours so I built this: https://github.com/zertosh/beautify-with-words. It unminifies code using UglifyJS2 but uses a phonetic word generator to rename variables. You get "long-ish" variable names so it's a breeze to do a find-and-replace. Hope this helps someone else!
You might have another way out.
Check out the last unminified version of the code. Compare to the minified version. Arguably most of it should be the same modulo consistent variable renaming. The differences you'll have to rename and remerge.
Diff won't do this kind of compare; you need tools that compare the programs as code, not text. Our SmartDifferencer tool will do this (by using language-specific full parsers to generate ASTs, and then comparing the ASTs); in effect, it compares the programs in spite of whitepspacing. SmartDifferencer also handles renaming; if two file are identical modulo a single renaming, that's what SmartDifferencer tell you.
I don't know how well this work work out; we haven't tried SmartDifferencer with 6000 lines of "consistently renamed" variables.
I found that a Visual Studio extension we've licensed here called "Telerik JustCode" has functionality to do what I want.

How would I solve a coding puzzle with Javascript?

There is a website called Gild.com that has different coding puzzles/challenges for users to do. They can be completed in wide array of languages including Javascript. I am interested in solving these puzzles in Javascript, but I am unsure of the following:
How am I supposed to access the input file which is supposed to be passed as an argument?
How am I supposed to output the result?
My understanding of Javascript is that it is run from within an HTML page and that output really is only in the form of placing values in the HTML, modifying the DOM, etc. For that reason it is not clear to me how Javascript can be used for solving these types of problems. Can someone who has used Gild before or has some insights into my question suggest how to proceed?
An example of a problem would be: the given input file contains a positive integer, find the sum of all prime numbers smaller than that integer and output it.
EDIT: Some of the solutions below involve using external resources, but on Gild, I am supposed to put my solution in their editor and then submit it that way, like the following picture shows:
In other words, I don't think my solution can have access to Node.js or other external resources.
Edit: Here are some interesting articles that I have found that I think are the answer to my question:
http://www.phpied.com/installing-rhino-on-mac/
http://www.phpied.com/javascript-shell-scripting/
I haven't spent much time on Gild, but I do a lot of similar types of problems on Project Euler. I think the best way to go is probably Node.js.
If you're not familiar, Node is basically server-side JavaScript that runs in Google's V8 engine. Installing it on your own Mac/Windows machine takes about 2 minutes. It's also really fast (considering it's JavaScript).
And you'd use it like this:
var fs = require('fs'); // the filesystem module
var contents = fs.readFileSync('theFile.txt', 'utf-8');
// Do stuff with the file contents...
Everything after those first two lines can be done with the same JS you'd write in the browser, right down to calling console.log() to spit out the answer.
So, if you wrote your script in a file on your desktop called getprimes.js, you'd open up your terminal and enter node ~/Desktop/getprimes.js (assuming you're on a Mac)
If you're:
on a Mac,
planning to do a lot of these puzzles, and
willing to pay $10, then
I highly recommend CodeRunner. It encapsulates runtimes for a variety of languages — from C to JavaScript — and lets you quickly build and run any sort of one-off code. Just hack together your code, ⌘R, and the results are printed right there in the same window.
I haven't used any file-based JavaScript in CodeRunner, but I imagine kennis's suggestions would apply. To output your results:
console.log(...)
Easy as pie!

How to find functions/operators/methods in JS files quickly and easily

I'm looking for an app or a command line tool that can help me quickly find a defined function in a file. The file in question here is the EXT-debug.js file. I want to override some methods(in this case onRender) however I need to figure out the signature of said functions.
I've yet to try Eclipse or Aptana; I'm looking for a more lightweight solution.
I use agent ransack. It's able to search for files as well as content. I also like the fact that you can run it on demand as it does not use an invasive, indexing service.
Agent Ransack
Visual Studio is not exactly a lightweight solution, but I have to recommend it. It's come a long way towards becoming an excellent JavaScript editor.
Visual Studio's IntelliSense is able to infer JavaScript types and give you a dropdown of the functions and values in your object (works pretty well).
Of course, I always have my handy little muscle memory spasm: double-click-select -> Ctrl-C -> Ctrl-Shift-F -> Ctrl-V -> ENTER
And here's a plugin that pre-searches for exact string matches in your entire solution and highlights them for you, so jumping to a function definition is instantaneous. Whole Tomato's Visual Assist X

Categories