Eclipse JavaScript code formatter and JSHint anonymous function format conflict - eclipse

I am using the Eclipse (version Indigo) JavaScript code formatter and using the jshint-eclipse plugin with white: true option for code convention validation.
Eclipse code formatter and JSHint plugin conflict with the anonymous function declaration format.
The JavaScript code formatter formats anonymous functions like the following:
var f1 = function() {
};
But the jshint-eclipse plugin gives a "Missing spaces after function" warning.
The right format for this plugin is:
var f1 = function () {
};
NOTE THE SPACE AFTER THE function
Is there a way to format anonymus function declaration differently with eclipse than regular function declarations. I would like to add one space after "function" for anonymous functions but not for normal functions.
Thanks.
Update a relevang eclipse bug is here

There is a bug for this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=315507
Similar bug in Aptana was fixed: http://jira.appcelerator.org/browse/APSTUD-3792
I've worked out a patch for this: https://github.com/eclipse/webtools.jsdt.core/pull/1
which hopefully will be merged and released soon.

Check the JavaScript formatting preferences (Preference->JavaScript->Code Style->Formatter) on the White Space tab, for the Declarations of Functions.

Related

Atom-Editor: Failed to load init.coffee - ERROR: reserved word 'function'

UPDATE: I found a solution/workaround for the problem. I renamed init.coffee to init.js because Atom also supports JavaScript. I would still like to know the cause of the problem. Is the script below not valid CoffeeScript or am I missing some dependence? I Installed Atom from the official Arch repositories.
For some reason my init.coffee cannot be loaded. The following code is from Atom's "Composed" Commands documentation:
atom.commands.add('atom-text-editor', 'custom:cut-line', function () {
const editor = this.getModel();
editor.selectLinesContainingCursors();
editor.cutSelectedText();
});
Atom throws an error when it starts:
Failed to load /home/myname/.atom/init.coffee
reserved word 'function'
I'm not sure if this is a bug, my fault, or a result of out-of-date documentation. The error message isn't super helpful, since I already that "function" is a reserved word, even though I don't know a lot of Coffee/JavaScript.
I replaced function using () -> {...}, which resulted in the same error except this time for the reserved word const.
Finally, I tried defining a named function which I passed as an argument to atom.commands.add() and got the same error.
I'm on Linux. atom --version returns:
Atom : 1.46.0
Electron: 4.2.12
Chrome : 69.0.3497.128
Node : 10.11.0
Your solution is the right direction - that code is JavaScript, not CoffeeScript.
It looks like the '"Composed" Commands' documentation you referenced is using both JavaScript and CoffeeScript in their examples.
To convert from JavaScript:
atom.commands.add('atom-text-editor', 'custom:cut-line', function () {
const editor = this.getModel();
editor.selectLinesContainingCursors();
editor.cutSelectedText();
});
to CoffeeScript:
atom.commands.add 'atom-text-editor', 'custom:cut-line', () ->
editor = #getModel()
editor.selectLinesContainingCursors()
editor.cutSelectedText()
When calling a function with arguments, you can leave out the parenthesis ().
function is removed in CoffeeScript, just use parenthesis and either a single -> or double arrow =>, where a double arrow is the same as .bind(this), so that would be incorrect here.
No const/let/var keywords. Just defined the variable without them.
this. can be replaced with #.
Braces ({}) wrapping function definitions are optional.
No semicolons.
If you want to learn CoffeeScript and help the community, you could fix the documentation yourself by forking, editing and making a pull request of the repository.
Alternatively, you should report this error in the documentations in their repository as an issue.

How to prevent VS Code from putting '{' on new line in C++ formatting?

If I write some code
void function() {
....
}
when I format the entire page using Shift+Alt+F, it becomes
void function()
{
....
}
How do I prevent that initial opening bracket from going to a new line?
Assuming you're using the MS C++ extension, these are the docs you're after. In > short, you'd need to either:
change C_Cpp.clang_format_fallbackStyle to one of: LLVM, Google, Chromium, > Mozilla, WebKit - and see if one matches your preferences.
find/make a custom .clang-format file
see clang-format docs for more details: > https://clang.llvm.org/docs/ClangFormatStyleOptions.html
https://www.reddit.com/r/vscode/comments/9rqj02/prevent_vscode_from_putting_c_curly_braces_on_new/

adding syntax highlighting to Jupyter notebook cell magic

I'm trying to figure out how to activate CodeMirror syntax highlighting for a CodeMirror-supported language (cypher) within a cell for a custom Jupyter cell magic (%%mymagic). The magic isn't associated with a special kernel - it just runs Python commands that process the string entered into the cell that I want to highlight. From what I can tell, this ostensibly can be done using something like
from notebook.services.config.manager import ConfigManager
cm = ConfigManager()
cm.update('notebook', {'CodeCell': {'highlight_modes': {'magic_cypher': {'reg': '^%%mymagic'}}}})
within the class that implements the magic.
I can't seem to get this to work, however; no change in highlighting occurs when I enter stuff in a cell that starts with %%mymagic. Is the above approach accurate? Does 'magic_cypher' need to have a specific format? Does the magic need to somehow specify the MIME type CodeMirror associates with the desired highlighting language? I'm using notebook 5.0.0, jupyter_core 4.3.0, and python 2.7.13.
The following code works for SQL when placed in ~/.jupyter/custom/custom.js with notebook 5.x:
require(['notebook/js/codecell'], function(codecell) {
codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
Jupyter.notebook.get_cells().map(function(cell){
if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
});
});
Credit goes to Thomas K for this info!
The case where I've been successful doing this was in adding SQL highlighting for the %%sql magic. I did this by adding the following to ~/.jupyter/custom/custom.js. The first line adds the mode to the Codemirror configuration, the rest apply the style to any existing cells in the workbook that need it (later cells will get styled appropriately as they are created). I haven't been successful in having it happen when the magic is installed, although I expect that it is possible.
IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
IPython.notebook.events.one('kernel_ready.Kernel', function(){
IPython.notebook.get_cells().map(function(cell){
if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
});

How do I tweak how VSCode format JavaScript?

How do I make Visual Studio Code insert a space after the function keyword, when I autoformat a JavaScript file with Alt+Shift+F?
// How I would like it:
var f = function (a) { ... }
// How VSCode is formatting by default:
var f = function(a) { ... }
I would assume this is possible placing a file in ~/.vscode/extensions/javascript, possibly by making a copy of VSCode's JavaScript.tmLanguage file and making a small change in it, but that's as far as I have gotten for now.
If you are using VS Code's built-in JS/TS formatter, this is controlled by:
"javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true
These are the default values.

Can the Eclipse code style formatter align assignment statements?

Eclipse has an option in Indentation: "Alignment of fields in class declarations" that will
align assignments of class members as in:
class Foo {
int x = 3;
String y = "abc"
long z = 2;
}
Is there a setting that will do the same for method level assignments as in:
private void foo() {
int x = 3;
String y = "abc"
long z = 2;
}
I can not find such a setting.
Try creating a new Java Code Style Formatter profile (Window->Prefernces->Java->Code Style->Formatter) or modifying an older one and check Indentation and Braces tabs.
For future visitors.
There is no such option in Eclipse, but, as mentioned here, you can use plugin named OCDFormat that aligns assignments and declarations in a selected piece of code.
Installation: download the latest version of OCDFormat_*.jar from project page on github (click → Raw → Save as) and add it to your Eclipse plugins or dropins directory.
Usage: select a piece of code and press Ctrl+4.
The Eclipse code style formatter cannot align assignment statements.
columns4eclipse is plug-in that allows you to do so. I had made a gif video illustrating its use but my answer got deleted by a moderator (as >10k users will see), so I let you try the plug-in and see by yourself.