How to configure NeoVim Treesitter in VimScript? - neovim

I'm trying to configure treesitter syntax highlighting for my neovim config but the docs only show examples with Lua and i am using Vimscript. If you're using VimScript they redirect you to an example of calling a Lua function within VimScript but I don't understand how it works.
From their docs:
Following examples assume that you are configuring neovim with lua. If you are using vimscript, see :help lua-heredoc. All modules are disabled by default and need to be activated explicitly in your init.lua, e.g., via
lua-heredoc:
Executes Lua script {script} from within Vimscript. {endmarker} must NOT
be preceded by whitespace. You can omit [endmarker] after the "<<" and use
a dot "." after {script} (similar to |:append|, |:insert|).
Example: >
function! CurrentLineInfo()
lua << EOF
local linenr = vim.api.nvim_win_get_cursor(0)[1]
local curline = vim.api.nvim_buf_get_lines(
0, linenr - 1, linenr, false)[1]
print(string.format("Current line [%d] has %d bytes",
linenr, #curline))
EOF
endfunction
Note that the `local` variables will disappear when the block finishes.
But not globals.
I'd like to make this Lua code work in VimScript:
require("nvim-treesitter.configs").setup({
ensure_installed = { "javascript", "typescript", "lua", "vim", "json", "html", "rust", "tsx" },
sync_install = false,
auto_install = true,
highlight = {
enable = true,
},
})

Just add the following to any .vim file which will be loaded:
lua << EOF
require("nvim-treesitter.configs").setup({
ensure_installed = { "javascript", "typescript", "lua", "vim", "json", "html", "rust", "tsx" },
sync_install = false,
auto_install = true,
highlight = {
enable = true,
},
})
EOF
and it'll be normally called as if you'd be in a lua file. You can imagine that as a wrapper vor neovim which says at lua << EOF: "Ok bro, after this line, there'll be lua code and not vimscript stuff. Got it?" and if it reaches the bottom EOF: "Alright, the input for the lua command ends here. We can keep going after this line with vimscript!"
Other example
Take a look into that as an example:
lua << EOF
print("Hello from lua")
EOF
If you open up neovim in a new file now then you can see a Hello from lua in your messages (:msg).
(Opiniated) Recommondation
I highly recommend to use an extra lua file where you write your lua-code to avoid those "wrappers" (lua << EOF and EOF) in your config file which makes it more readable in my opinion.

Related

C programming on PI using VS-Code. Get "undefined reference to '...' " when using the quote form of the include directive

I would like to program a PI via VS-Code. When I translate the program I get "undefined referenc to `...' ". It seems that the header files that I include via #include "..." are not translated. If I translate the data in the terminal and then link the headers afterwards, it works. If I have understood correctly, you can set this via the tasks.json. However, I have not yet found out how.
I would like to modify an existing C program that has already been programmed on a PI. The programming is to be done in VS-Code via SSH. However, as soon as I try to run the program, I get numerous error messages with "undefined reference to '...' ".
Even with the minimal program helloworld I get this notification as soon as I want to include local .h-files with #include "...". System files with #include <..> are found.
The following programme
#include <stdio.h>
int main()
{
printf("Hello World\n");
}
is translated without problems.
However, as soon as I add Messages.h
#ifndef MESSAGES_H_INCLUDED
#define MESSAGES_H_INCLUDED
void printMsg(char *str);
#endif // MESSAGES_H_INCLUDED
and Messages.c
#include "Messages.h"
void printMsg(char *str)
{
printf("Testausgabe: %s\n",str);
}
to the project and to the program
#include <stdio.h>
#include "Messages.h"
int main()
{
printf("Hello World\n");
printMsg("Somthing else.\n");
}
The compiler returns
/usr/bin/ld: /tmp/ccBVuwoo.o: in function `main':
helloworld.c:(.text+0x14): undefined reference to `printMsg'
collect2: error: ld returned 1 exit status
And this although all files are stored in the same folder.
If I create the files manually using the terminal and then link them, it works.
$ gcc -c helloworld.c
$ gcc -c Messages.h
$ gcc -o Hello helloworld.o Messages.o
Does this mean that I have to manually create a make file that I have to expand for each new header (that would be a bummer)? Couldn't this also be done automatically via VS-Code? Have I perhaps overlooked something here? Sorry, but I am very new to this.
I am grateful for any help.
Attached is also the automatically generated tasks.json from VSC.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc Aktive Datei kompilieren",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Vom Debugger generierte Aufgabe."
}
],
"version": "2.0.0"
}

Babel preset es2015 loose not working?

Is this "loose": true not working for the "es2015" preset or a mistake/misunderstanding on my part?
Input Code
I've tried with the REPL and on on the command line. I can't get babel to translate my loose javascript in looseWith.js:
var obj = {};
with (obj) {
}
.babelrc (attempt1):
{
"presets": [
[ "es2015", { "loose": true }]
]
}
.babelrc (attempt2 - as in REPL):
{
"presets": [
"es2015-loose"
]
}
I then try running either of these .babelrcs:
> babel looseWith.js
SyntaxError: looseWith.js: 'with' in strict mode (2:0)
1 | var obj = {};
> 2 | with (obj) {
| ^
3 | }
Seems to me that this is still in strict mode. The documentation on loose is quite scarce and just says:
Enable "loose" transformations for any plugins in this preset that allow them.
I'm trying to negate use strict :-) Is this my misunderstanding? What other meanings are there for "loose"?
I then tried adding "modules": "umd" to .babelrc to attempt1 above, and when fed with a proper strict .js file, it did produce umd output, so I think babel is picking up the .babelrcmodule just fine.
Background
I'm trying to be able to use ES6 in my underscore/lodash templates. The javascript output of e.g. _.template(script).source contains the "with" statement (by default). So I'm trying to use babel to translate ES6 - including the "with" statement - into ES5.
Environment
> node --version
v7.10.1
> cat package.json
{
"dependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2015-loose": "^8.0.0"
}
}

Escape $ in snippet

I'm trying to escape the $ character in my snippets but I cannot seem to get it correct.
If I use \$ as it looks like I should be from the documentation, I get the error:
file: path/to/snippets/php.json'
severity: 'Error'
message: 'Invalid escape character in string'
And no $ appear in my snippet.
If I use $$ found from this answer, I get a $ to show up but it thinks that the text immediately following is a tabstop.
If I use $\ it works but I have to have a character that is part of an escape sequence immediately following. So if I wanted $factory, I would need to do $\ffactory. \f seems to be the best as it does not effect the layout of my snippet.
I'm pretty sure that I am missing what needs to be done here.
My snippet for reference:
"factory" :{
"prefix": "factory",
"body": [
"\$factory->define($1, function (Faker\\Generator \$faker){",
"\treturn [",
"\t\t$2,",
"\t];",
"}"
],
"description": "Creates Model factory"
},
After much trial and error, I found that using \\$ will give me the desired results. So if I wanted $test in my snippet output, with test not being a tabstop, I would need \\$test snippet definition body:
"factory" :{
"prefix": "factory",
"body": [
"\\$factory->define($1, function (Faker\\Generator \\$faker){",
"\treturn [",
"\t\t$2,",
"\t];",
"});"
],
"description": "Creates Model factory"
},

JSDoc output inconsistent between gulp-jsdoc & standard CLI

I'm trying to build docs for a simple set of JS code (given below). If I use gulp, the docs are created how I would expect them. If I use the CLI, the docs are incomplete.
Here's my JS code:
// BASE.js
/** #module BASE */
var BASE = {};
// MOD1.js
/** #class MOD1 - Test module */
BASE.MOD1 = Object.create({});
/**
* Just a test function
* #param {Object} var1 - A test variable
*/
BASE.MOD1.testFunction = function(var1){
alert('hi');
};
My gulp file:
var gulp = require('gulp'),
jsdoc = require('gulp-jsdoc'),
outDir = './gulp-docs/',
docInfo = {},
docOptions = {},
docTemplate = {},
srcFiles = [
"BASE.js",
"MOD1.js"
];
gulp.task('default', function() {
return gulp.src(srcFiles)
.pipe(jsdoc.parser(docInfo))
.pipe(jsdoc.generator(outDir, docTemplate, docOptions))
});
And my command line:
C:\DocTest> jsdoc BASE.js MOD1.js --configure rawconf.json --destination raw-docs
rawconf.json:
{
"tags": {
"allowUnknownTags": true
},
"plugins": [],
"templates": {},
"opts": {
"package": "./rawpackage.json"
}
}
rawpackage.json:
{}
I run both gulp and the jsdoc command from the Node.js command prompt.
Output from gulp is the following files:
BASE.js.html
BASE.MOD1.html
index.html
MOD1.js.html
module-BASE.html
Output from the CLI is the following files:
BASE.js.html
index.html
MOD1.js.html
module-BASE.html
module-BASE-BASE.MOD1.html
There are some small differences which I can chalk up to the differences between the gulp-jsoc version of jsdoc (3.3.0-alpha5) and the current version (3.3.0-beta3).
But the biggest difference is that while in the gulp output, I can find information on testFunction, there is no information to be found at all regarding testFunction anywhere in the CLI output. I've even searched the HTML code--nothing.
So did I do something wrong? I'm just trying to achieve parity at this point, and I've exhausted any documentation I could find online.
If you look at the gulp-jsdoc github page here, there's a "Big Fat Warning" that this plugin isn't being kept up to date.
Try using the gulp-shell plugin. You can use exactly what you typed into the command line.

r.js throws an error every time I try to assign a variable in Coffee-Script

So I'm using r.js to build a bunch of my files -- some of which are Coffee-Script. I am using the Require plugin require-cs to handle this.
Here is a look at my Require.js config, a la rjs:
rjs.optimize({
baseUrl: SRC_PATH,
include: channelMap[channel],
optimize: 'none',
stubModules: ['cs', 'tpl', 'less', 'text'],
exclude: ['normalize', 'coffee-script', 'underscore'],
CoffeeScript: {
header: false,
// since we use AMD, there's no need for IIFE
bare: true
},
separateCSS: true,
skipModuleInsertion: true,
// If something needs to be present for tests too and not only for
// the build step, then add it tools/karma-amd.js instead
paths: _.extend({
'less-builder': 'vendor/require-less/less-builder',
'normalize': 'vendor/require-less/normalize'
}, rjsPaths),
wrap: true,
less: {
paths: [path.join(BASE_SHOP_FOLDER, 'static', 'zalando', 'css', channel)]
},
out: path.join(BUILD_PATH, channel, BUILD_BASE_FILE_NAME + '.js')
}, function () {
// this needs to be async because less builder uses
// process.nextTick() to write the file
process.nextTick(done);
});
Even the most simple .coffee file seems to fail violently. E.g.
define [], ->
foo = "hello world"
return foo
throws the following error:
the variable "foo" can't be assigned with undefined because it has not been declared before
foo = "hello world"
^^^
When I use replace require-cs's coffee-script.js with the older version of 1.6.3 everything works just fine.
Your code compiles BTW. Try to go to CoffeeScriptDahWebSite and click on TRY COFFEESCRIPT and you will see that it is valid code.
From the define [], () -> code ..., I assume you are using the CoffeeScript plugin with require.js. I am ready to bet your issue is in the require.js configuration (which should be your main.js file or whatever you named it) since the error you get looks oddly like the JavaScript interpreter trying to run the invalid code you wrote (for JavaScript that is :). Meaning, your plugin is not there at all.
If you give me your require configuration maybe I can edit this answer and help you more.
Cheers!
EDIT
I see you edited your question, but you provided me the wrong file. What you showed me was the r.js optimizer configuration, instead of the main.js which specifies how cs.js and coffee-script.js files are loaded. The error might be in your optimizer, but I can't know without seeing your other config.
A reiteration of that, show me the entry point of your program, the data-main that is loaded in your HTML.
I was unable to recreate the issue:
$ cat ./etc/temp1.coffee
define [], ->
foo = "hello world"
return foo
$ coffee --version
CoffeeScript version 1.7.1
$ which coffee
/home/dev/.nvm/v0.10.23/bin/coffee
$ coffee -cp ./etc/temp1.coffee
// Generated by CoffeeScript 1.7.1
(function() {
define([], function() {
var foo;
foo = "hello world";
return foo;
});
}).call(this);
$ coffee -cpb ./etc/temp1.coffee
// Generated by CoffeeScript 1.7.1
define([], function() {
var foo;
foo = "hello world";
return foo;
});
Turns out the problem was with my previous version of 1.7.1. Someone Beautified it and broke everything. Everything works as advertised when I go out of my way to get coffee-script.js from http://coffeescript.org/extras/coffee-script.js