Is it possible with Eclipse to only find results in string literals? - eclipse

Lets assume I have the following Java code:
public String foo()
{
// returns foo()
String log = "foo() : Logging something!"
return log;
}
Can I search in Eclipse for foo() occurring only in a String literal, but not anywhere else in the code? So in the example here Eclipse should only find the third occurrance of foo(), not the first one, which is a function name and not the second one, which is a comment.
Edit: Simple Regular Expressions won't work, because they will find foo() in a line like
String temp = "literal" + foo() + "another literal"
But here foo() is a function name and not a String literal.

You can try it like this:
"[^"\n]*foo\\(\\)[^"\n]*"
You have to escape brackets, plus this regex do not match new lines or additional quotes, which prevent wrong matches.

Maybe you should use regex to find any occurence of foo() between two " ?

Related

Eclipse formatter: How to move comment between method name and open brace to always have open brace on same line as method declaration

I have code that looks like this, that I'm trying to format
Original code:
public int doThing(int a) // -incredibly useful comment here
{
int ab = a+1;
return ab;
}
I want it to look like this
public int doThing() { // -incredibly useful comment here
int ab = a+1;
return ab;
}
If I try to turn on the Brace position -> Method Declaration -> Same line option and run the formatter, any code with a comment in the position "breaks" the formatter, and I get an output for my example that looks the same as the original code, but methods without a comment have the correct formatting (meaning the results are inconsistent).
Is it possible with the eclipse formatter to get the style I want? I'm trying to run it against a large amount of code, and would prefer not to have to fix these all manually to get a consistent brace position.
The problem here is that is not formatting but rewriting. Using File Search + regular expression + Replace could do that in bulk.
Try this regex
^(\s*(?:public|private|protected)\s+[^(]+\([^)]*\))(\s*\/\/[^/]+)\R\s*\{
On File Search ( Ctrl + H)
Hit Replace and use $1 { $2\n as replacement
Code should compile after the refactoring.
UPDATE:
Fixed regex part that represents function arguments
\([^)]*\)
Full Regex matches these cases
public int doSmthg() // coment here
{
return 1;
}
private String doSmthgElse(String arg) // coment here
{
return arg;
}

Can I have a string containing a delegate that is evaluated at runtime?

Can I have a string that contains a delegate that gets expanded at various times during runtime?
$pattern = "(?m)^INFO\:(?:\s|\t)*$({script:$marker})\:(?:\s|\t)*(?<url>.*)$"
$marker = "Some marker value"
:
#Do something with the resulting pattern containing the marker value
:
$marker = "Some other marker value"
:
#Do something with the pattern having the new marker value
and so on... I'd prefer not to have to keep redefining the string... or having a function that builds it. It seems so much more succinct if I could just have a few characters in the string that get evaluated when the string is needed vs. when the $pattern value is set.
you can do
$pattern = {"(?m)^INFO\:(?:\s|\t)*($script:marker)\:(?:\s|\t)*(?<url>.*)$"}
and then later use
$pattern.invoke()
(Assuming you want $script:marker to be the characters that get set later, your original example has $({script:$marker}), but that won't work if it is supposed to do what I think it should ;))
In general: Define the term as Scriptblock using {} and later .invoke() to evaluate it.
Just make sure there is no confusion about the types within the curly brackets, otherwise you might get some strange results...

String.isSubstring dosnt work - sml

I want to check if a line contains a specific word, so I tried to use the String.isSubstring function with the line and the specific word. But somehow that function dosn't work for me.
String.isSubstring("Hi my name is...", "name");
stdIn:2.1-2.47 Error: operator and operand don't agree [tycon mismatch]
operator domain: string
operand: string * string
in expression:
String.isSubstring ("Hi my name is...","name")
-
I will love if some one can tell me what did i do wrong?
Thanx
String.isSubstring is a curried function -- that is, its arguments are passed in separately, not as a tuple.
Try
String.isSubstring "Hi my name is..." "name"

How do you write an empty while loop in Coffeescript?

I'm trying to translate some old code to Coffeescript. But there is no direct translation for:
while ( doWork() ) {}
"while doWork()" with nothing after it results in a syntax error.
while doWork() then
Should do the trick
using then is probably the canonical solution since it is explicitly meant for separating the condition from the (in this case empty) body. Alternatively you can write
while doWork()
;#
(the # keeps vim syntax highlighting from flagging it as an error)
I also like the continue while doWork() solution, but I strongly advise against any other form of expression while doWork() mentioned in the comments since when this is the last statement of a function it will become a list constructor:
_results = [];
while (doWork()) {
_results.push(expression);
}
return _results;

; expected but <place your favourite keyword here> found

I'm trying to write a class for a scala project and I get this error in multiple places with keywords such as class, def, while.
It happens in places like this:
var continue = true
while (continue) {
[..]
}
And I'm sure the error is not there since when I isolate that code in another class it doesn't give me any error.
Could you please give me a rule of thumb for such errors? Where should I find them? are there some common syntactic errors elsewhere when this happens?
It sounds like you're using reserved keywords as variable names. "Continue", for instance, is a Java keyword.
You probably don't have parentheses or braces matched somewhere, and the compiler can't tell until it hits a structure that looks like the one you showed.
The other possibility is that Scala sometimes has trouble distinguishing between the end of a statement with a new one on the next line, and a multi-line statement. In that case, just drop the ; at the end of the first line and see if the compiler's happy. (This doesn't seem like it fits your case, as Scala should be able to tell that nothing should come after true, and that you're done assigning a variable.)
Can you let us know what this code is inside? Scala expects "expressions" i.e. things that resolve to a particular value/type. In the case of "var continue = true", this does not evaluate to a value, so it cannot be at the end of an expression (i.e. inside an if-expression or match-expression or function block).
i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
}
This is a problem, as the function block is an expression and needs to have an (ignored?) return value, i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
()
}
() => a value representing the "Unit" type.
I get this error when I forget to put an = sign after a function definition:
def function(val: String):Boolean {
// Some stuff
}