I'm trying to get the funcList visual studio code extension plugin to work with a proprietary language that I use and I'm having a problem getting the regex to work. The plugin documentation can be found here: https://marketplace.visualstudio.com/items?itemName=qrti.funclist and describes creating a settings.json file inside the .vscode folder of your project. My problems arise when trying to modify the regex expressions for the funcList.nativeFilter and funcList.displayFilter configuration values. Here is my current settings.json file:
{
"funcList.nativeFilter": "/(?:^|\\s)Function\\s+\\w+\\(/mg",
"funcList.displayFilter": "/\\s*Function\\s+(.*)/1",
"funcList.sortList": 1,
"funcList.doubleSpacing": false
}
I believe the main problem is the part of \\w+ in the nativeFilter property. This seems to only match on characters/numbers but not any special characters. Here is a snippet of a piece of code that I would like to work with this Function List:
Function Do.Something(paramOne, paramTwo)
'...
End Function
Method Do.Something_Else(paramOne, paramTwo)
'...
End Function
Ideally, the nativeFilter would capture Function Do.Something(paramOne, paramTwo) .. until End Function and then the displayFilter would only capture the first line (ie. Do.Something(paramOne, paramTwo))
Note: according to the docs, nativeFilter does not allow regex groups, but displayFilter allows groups 0-9.
I use this for javascript and the funcList extension. It is similar to what you are trying to do:
// so that "function someName(arg1, arg2) is captured
"funcList.nativeFilter": "/^[a-z]+\\s+\\w+\\s*\\(.*\\)/mgi",
// now display "someName(arg1, arg2)"
"funcList.displayFilter": "/\\S* +(\\w+\\s*\\(.*\\))/1",
You obviously have capital letters, periods and underscores to worry about but hopefully this helps you to some degree. [I see I did not need to actually put the function keyword in to make it work...]
[EDIT] Try this, it seems to work:
"funcList.nativeFilter": "/^Function \\w+.\\w+\\(.*\\)?/mg",
"funcList.displayFilter": "/\\S* +(\\w+.\\w+\\(.*\\))/1",
You just needed the . as it is not included in \w
This seems to be working for my needs:
{
"funcList.nativeFilter": "/^(Function|Method|Macro)\\s+[a-zA-Z0-9.+(){}\\/\\\\[\\],_\\-=:;!##$%^&*|,.<>? ]*\\).*$/mg",
"funcList.displayFilter": "/\\s*((Function|Method|Macro)\\s+[a-zA-Z0-9.+(){}\\/\\\\[\\],_\\-=:;!##$%^&*|,.<>? ]+)/1"
}
With this plugin, you need to capture the entire line in order to get the navigation to work when clicking on a function list.
Related
I want to use the value of my variable in Go template as-is but Go is adding extra quotes around it. E.g., for a Go template like
{{.Site}}:{{.Port}}/{{.Path}}
I want to get the output as
Mysite:3000/from/here
but the template is giving me the following instead:
"Mysite":"3000"/"from/here"
So,
How can I fix it (get rid of all the extra quotes or better suspend them all)? See https://play.golang.org/p/uKpgXdLv5gM
Go template also changed "orgId=1&refresh=30s" to orgId=1\u0026refresh=30s, how to avoid that?
Moreover, if I add https:// to the front of my url, the output is truncated. How to fix that as well?
Finally, is it possible to escape "`" within "`"?
As per Go HTML template doc:
HTML templates treat data values as plain text which should be encoded so they can be safely embedded in an HTML document. The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts.
The security model used by this package assumes that template authors are trusted, while Execute's data parameter is not. More details are provided below.
It means JavaScript escaping is enabled whenever the go HTML template engine detects that it is within a <script> tag, (i.e., it has nothing to do with whether using regular " or not as the first commenter thinks). So
to get the output as
Mysite:3000/from/here
instead of:
"Mysite":"3000"/"from/here"
Do not wrap it with <script> & </script> tag.
Do the concatenation after template Execute().
Again, with <script> & </script> tag wrapped around, I'm getting:
var url = `"Mysite":"3000"/"from/here"/${othervars}?"orgId=1\u0026refresh=30s"`
vs. without <script> & </script> tag wrapped around it, I'm getting:
var url = `Mysite:3000/from/here/${othervars}?orgId=1&refresh=30s`
Just what I need.
However, my actual case is that I'm using go HTML template engine to process my .html template files, so I cannot really do the concatenation afterwards, as everything is defined in the .html template file. So, just as Martin Gallagher has shown in his code, for such case, using template function seems to be the only option.
But even that might not be a viable option, as this is what I'm getting out of Martin's code:
var url = "Mysite:3000\/from\/here?orgId=1\u0026refresh=30s"
It is still not exactly what I wanted:
var url = `Mysite:3000/from/here/${othervars}?orgId=1&refresh=30s`
So maybe with such case, it indeed has no ideal solution.
I faced with the problem of writing my vcs current branch name each time I have written 'todo' comment.
Recently I learned about Intellij's 'Live Templates' which is quite comfortable to use. I tried to apply it to my problem but there's no templates to take out a branch name.
So the question is could I actually take out the name of my branch to code comments somehow?
It is possible to use the groovyScript predefined function and a script to extract the branch name. For example create the following live template:
$COMMENT$ todo [$BRANCH$]: $END$
with abbreviation "todo" and description "Inserts todo comment with branch name". Click Edit variables and give the variables the following definitions:
COMMENT:
lineCommentStart()
BRANCH (updated for 2020.2 and newer)
groovyScript("com.intellij.dvcs.repo.VcsRepositoryManager.getInstance(_editor.project).getRepositoryForFileQuick(com.intellij.openapi.fileEditor.FileDocumentManager.getInstance().getFile(_editor.document)).getCurrentBranchName()")
Skip if defined checked for both variables. The Groovy script is (unfortunately) all one line. Set applicable contexts to Everywhere.
With this live template it is now possible to type todoTab somewhere in a source file and a line comment with the branch name will be inserted. This will insert the proper line comment depending on the language of the file, or nothing in case of languages without a line comment like HTML. And should extract the branch name no matter the type of version control used (I tested with Git).
For live templates you can use predefined functions. Unfortunately there is no function to detect the current VCS branch.
But you can create a template to make work a little easier:
// TODO [$branch_name$]: $comment$
With this template, you still have to fill branch name, but you should not type symbols like [ and caret will be placed automatically.
You can also create a feature request for a new predefined function.
In my project Mockito.times(1) is often used when verifying mocks:
verify(mock, times(1)).call();
This is redundant since Mockito uses implicit times(1) for verify(Object), thus the following code does exactly what the code above does:
verify(mock).call();
So I'm going to write an a structural search drive inspection to report such cases (let's say, named something like Mockito.times(1) is redundant). As I'm not an expert in IntelliJ IDEA structural search, my first attempt was:
Mockito.times(1)
Obviously, this is not a good seach template because it ignores the call-site. Let's say, I find it useful for the following code and I would not like the inspection to trigger:
VerificationMode times = Mockito.times(1);
// ^ unwanted "Mockito.times(1) is redundant"
So now I would like to define the context where I would like the inspection to trigger. Now the inspection search template becomes:
Mockito.verify($mock$, Mockito.times(1))
Great! Now code like verify(mock, times(1)).call() is reported fine (if times was statically imported from org.mockito.Mockito). But there is also one thing. Mockito.times actually comes from its VerificationModeFactory class where such verification modes are grouped, so the following line is ignored by the inspection:
verify(mockSupplier, VerificationModeFactory.times(1)).get();
My another attempt to fix this one was something like:
Mockito.verify($mock$, $times$(1))
where:
$mock$ is still a default template variable;
$times$ is a variable with Text/regexp set to times, Whole words only and Value is read are set to true, and Expression type (regexp) is set to (Times|VerificationMode) -- at least this is the way I believed it should work.
Can't make it work. Why is Times also included to the regexp? This is the real implementation of *.times(int), so, ideally, the following line should be reported too:
verify(mockSupplier, new Times(1)).get();
Of course, I could create all three inspection templates, but is it possible to create such a template using single search template and what am I missing when configuring the $times$ variable?
(I'm using IntelliJ IDEA Community Edition 2016.1.1)
Try the following search query:
Mockito.verify($mock$, $Qualifier$.times(1))
With $Qualifier$ text/regexp VerificationModeFactory|Mockito and occurrences count 0,1 (to find it when statically imported also).
To also match new Times(1) you can use the following query:
Mockito.verify($mock$, $times$)
With $times$ text/regexp .*times\s*\(\s*1\s*\) and uncheck the Case sensitive checkbox.
I'm pretty new to Elixir and functional programming-languages in general.
In Elixir, I want to call one specific function on Modules, given the Module name as String.
I've got the following (very bad) code working, which pretty much does what I want to:
module_name = elem(elem(Code.eval_file("module.ex", __DIR__), 0), 1)
apply(module_name, :helloWorld, [])
This (at least as I understand it) compiles the (already compiled) module of module.ex in the current directory. I'm extracting the modules name (not as a String, don't know what data-type it actually is) out of the two tuples and run the method helloWorld on it.
There are two problems with this code:
It prints a warning like redefining module Balance. I certainly don't want that to happen in production.
AFAIK this code compiles module.ex. But as module.ex is already compiled and loaded, it don't want that to happen.
I don't need to call methods on these modules by filename, the module-name would be ok too. But it does have to by dynamic, eg. entering "Book" at the command line should, after a check whether the module exists, call the function Book.helloWorld.
Thanks.
Well, thats where asking helps: You'll figure it out yourself the minute you ask. ;)
Just using apply(String.to_existing_atom("Elixir.Module"), :helloWorld, []) now. (maybe the name "Module" isn't allowed, don't know)
Note that you always need to prefix your modules with "Elixir."
defmodule Test do
def test(text) do
IO.puts("#{text}")
end
end
apply(String.to_existing_atom("Elixir.Test"), :test, ["test"])
prints "test"
and returns {:ok}
Here is a simple explanation:
Assuming that you have a module like this:
defmodule MyNamespace.Printer do
def print_hello(name) do
IO.puts("Hello, #{name}!")
end
end
Then you have a string that hold the module name that you can pass around in your application like this:
module_name = "Elixir.MyNamespace.Printer"
Whenever you receive the string module_name, you can instantiate a module and call functions on the module like this:
module = String.to_existing_atom(module_name)
module.print_hello("John")
It will print:
Hello, John!
Another way to dynamically call the function MyNamespace.Printer.print_hello/1 is:
print_hello_func = &module.print_hello/1
print_hello_func.("Jane")
It will print:
Hello, Jane!
Or if you want to have both module_name as atom and function_name as atom, and pass them around to be executed somewhere, you can write something like this:
a_module_name = :"Elixir.MyNamespace.Printer"
a_function_name = :print_hello
Then whenever you have a_module_name and a_function_name as atoms, you can call like this:
apply(a_module_name, a_function_name, ["Jackie"])
It will print
Hello, Jackie!
Of course you can pass module_name and function_name as strings and convert them to atoms later. Or you can pass module name as atom and function as a reference to function.
Also note that the name of a module is an atom so doing String.to_existing_atom isn't usually needed. Consider this code:
defmodule T do
def first([]), do: nil
def first([h|t]), do: h
end
In this case you can simply do the apply in this fashion:
apply(T,:first,[[1,2,3]])
#=> 1
Or this example (List below is the Elixir List module):
apply(List,:first,[[1,2,3]])
#=> 1
I mean to say that if you know the name of the module, it's not necessary to pass it as a string and then convert the string to an existing atom. Simply use the name without quotation marks.
Given something like:
org.jboss:jboss-remote-naming
...it would be useful to be able to transform it to:
exclude group: "org.jboss", module: "jboss-remote-naming"
Use case is exclusions for Gradle dependencies. Have no idea how to do this though. Could use some tips. I've been to the documentation often enough to know this probably isn't in there, but I will double check.
"exclude group: \""+ "org.jboss.logging:jboss-logging".replaceAll(':','\", module: \"') +"\""
...OK I've figured that you can do the above in the Groovy shell which results in the below:
groovy:000> "exclude group: \""+ "org.jboss.logging:jboss-logging".replaceAll(':','\", module: \"') +"\""
===> exclude group: "org.jboss.logging", module: "jboss-logging"
Now, how to get IDEA to use it?
Well, nobody has responded and I've found the answer anyway.
So the problem is that I've got strings like this on my clip board:
org.jboss.logging:jboss-logging
I'm using Gradle and IntelliJ IDEA. What I'm doing is fixing a big bunch of dependencies and I want to be able to paste exclusions in in the right form which is:
exclude group: "org.jboss.logging", module: "jboss-logging"
What I did was:
CTRL+ALT+S | Edit | Live Templates. Note that I've previously created a "User" group herein to put my personal Live Templates. I click "+" to add a new one to the User group.
Set Template Text to: $VAR$$END$
Select the Edit Variables button and set the Expression field to:
groovyScript("'exclude group: \"'+ _1.replaceAll(':','\", module: \"') +'\"'",clipboard())
Set the Abbreviation field to 'xld'.
Note the following regarding the text above:
A. groovyScript executes a short one line Groovy script (or Groovy file for a bigger script). See the docs here: Predefined Functions
B. Everything is wrapped in "" and then inside those double quotes, every time you want to use a double quote like so: ", you have to escape it with a \. The output has double quotes but aside from that I tried to stick to single quotes.
C. _1 is a string that represents the result returned by the clipboard() method.
How to use it: Copy any dependency of the form "org.jboss:jboss-remote-naming" then at the appropriate location in IntelliJ IDEA type xld and then [TAB]. It should expand using the proper syntax for a Gradle exclusion.