Skip to content

Commit

Permalink
Added more variants and improved occurrence highlighting, closes #240
Browse files Browse the repository at this point in the history
  • Loading branch information
de-jcup committed Mar 22, 2022
1 parent b719dac commit 4c57e05
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

## variant 1 - select "count", must appear multiple times

count=0

count=$count+1
count2=$count-1+
$count$xx
dividedcount=$count/2
${count}host

## variant2 - select "separator", must appear multiple times
separator=" "
$separator$host
${separator}host

## Other
echo "$separator$other"
echo "$separator"
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private IHyperlink[] createExternalFilelinks(BashFunctionEditorInfo functionInfo

private IHyperlink[] createExternalHyperlinks(BashFunctionEditorInfo functionInfo,
boolean canShowMultipleHyperlinks) {
/* not found internal, so try external variant */
/* not found internal, so try external variantFullText */

if (!BashEditorPreferences.getInstance().isSharedModelBuildEnabled()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private String buildHoverInfo(DocumentKeyWord keyword) {
sb.append("<b>Not available</b>");
} else {
if (TooltipTextSupport.isHTMLToolTip(tooltip)) {
/* it's already a HTML variant - so just keep as is */
/* it's already a HTML variantFullText - so just keep as is */
sb.append(tooltip);
} else {
/* plain text */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.jface.text.rules.Token;

/**
* Special variant, because boolean assignment to variables are not correct
* Special variantFullText, because boolean assignment to variables are not correct
* highlighted otherwise "myvar=true" was not highlighted but "myvar= true"
* which is wrong is highlighted
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void createTerminalParts() {
launchTerminalCommand.getTextControl(terminalGroup).setToolTipText("This represents " + TerminalCommandVariable.BE_TERMINAL.getVariableRepresentation() + " in start command before.\n"
+ "Defines the command to provide a login shell which can execute script\n" + "and keeps terminal open for debug output.\n\n" + "Here you can use "
+ TerminalCommandVariable.BE_CMD_CALL.getVariableRepresentation() + " as generated bash call script\n" + "and " + TerminalCommandVariable.BE_CMD_TITLE.getVariableRepresentation()
+ " for a title information in your terminal if you want it.\n\n" + "Press `Show result cmd` button to show calculated variant.");
+ " for a title information in your terminal if you want it.\n\n" + "Press `Show result cmd` button to show calculated variantFullText.");
addField(launchTerminalCommand);

Button showTestTerminalCommandButton = new Button(terminalGroup, SWT.PUSH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface BashDebugInfoProvider {

/**
* @return the path to user home relevant for script. When a custom script path to user home is defined this will be used, otherwise the {@link #getDefaultScriptPathToUserHome()}
* variant
* variantFullText
*/
String getResultingScriptPathToUserHome();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void inspectBashVariablesIfNecessary(BashVariableRegistry model, List<Pa
}

String normalUsage = "$" + variableToInspect.getName();
List<String> variantsIdentifiableByIndexOf = buildVariableIndexOfVariants(variableToInspect);
List<VariableUsageVariant> variantsIdentifiableByIndexOf = buildVariableVariants(variableToInspect);

Iterator<ParseToken> it2 = tokens.iterator();
while (it2.hasNext()) {
Expand All @@ -172,7 +172,7 @@ private void inspectBashVariablesIfNecessary(BashVariableRegistry model, List<Pa
int start;
int end;

for (String variant : variantsIdentifiableByIndexOf) {
for (VariableUsageVariant variant : variantsIdentifiableByIndexOf) {
int indexOf = -1;

do {
Expand All @@ -187,12 +187,15 @@ private void inspectBashVariablesIfNecessary(BashVariableRegistry model, List<Pa
/* avoid index of of bounds ...*/
break;
}
indexOf = text.indexOf(variant, indexOf + 1);
indexOf = text.indexOf(variant.variantFullText, indexOf + 1);
if (indexOf != -1) {
/* variantFullText found */
start = indexOf + token.getStart();
end = start + variant.variantFullText.length();

start = token.getStart() + indexOf;
end = start + variant.length();

start = start +variant.highlightStartAdd;
end = end -variant.highlightEndRemove;
addVariableUsage(variableToInspect, start, end);
}
} while (indexOf != -1);
Expand All @@ -209,8 +212,20 @@ private void addVariableUsage(BashVariable variableToInspect, int start, int end
variableToInspect.getUsages().add(usage);
}

private class VariableUsageVariant{
String variantFullText;
int highlightStartAdd;
int highlightEndRemove;

private VariableUsageVariant(String variant, int highlightStartAdd, int highlightEndRemove) {
this.variantFullText=variant;
this.highlightStartAdd=highlightStartAdd;
this.highlightEndRemove=highlightEndRemove;
}
}

/* builds a list of strings which identifies the bash variable usage */
private List<String> buildVariableIndexOfVariants(BashVariable variableToInspect) {
private List<VariableUsageVariant> buildVariableVariants(BashVariable variableToInspect) {
String variableName = variableToInspect.getName();
// the next line may not be used here directly, because we do not want
// "xxx" found inside "$xxxy" but only for "$xxx"
Expand All @@ -219,16 +234,22 @@ private List<String> buildVariableIndexOfVariants(BashVariable variableToInspect
// e.g. : "xxx" is represented by "$(xxx)abc"
String prefix = "$" + variableName;

List<String> variants = new ArrayList<>();
variants.add("$(" + variableName + ")");
variants.add("${" + variableName + "}");
variants.add(prefix + "[");
variants.add(prefix + " ");
variants.add(prefix + "'");
variants.add(prefix + "\"");
variants.add(prefix + "\\");
List<VariableUsageVariant> variants = new ArrayList<>();
variants.add(new VariableUsageVariant("$(" + variableName + ")",0,0));
variants.add(new VariableUsageVariant("${" + variableName + "}",0,0));
variants.add(new VariableUsageVariant(prefix + "[",0,1));;
variants.add(new VariableUsageVariant(prefix + " ",0,1));;
variants.add(new VariableUsageVariant(prefix + "'",0,1));;
variants.add(new VariableUsageVariant(prefix + "\"",0,1));;
variants.add(new VariableUsageVariant(prefix + "\\",0,1));;

variants.add(new VariableUsageVariant(prefix + "$",0,1));;
variants.add(new VariableUsageVariant(prefix + "+",0,1));;
variants.add(new VariableUsageVariant(prefix + "-",0,1));
variants.add(new VariableUsageVariant(prefix + "/",0,1));

for (char metaCharacter : BashMetacharacters.METACHARACTERS_WITHOUT_WHITESPACES) {
variants.add(prefix + metaCharacter);
variants.add(new VariableUsageVariant(prefix + metaCharacter,0,0));
}
return variants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BashVariableAssignment getInitialAssignment() {

/**
* Get the assignments in ordered way as defined inside script! So first entry
* here is also first (inital) assignment
* here is also first (initial) assignment
*
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private boolean handleStandardVariables(ParseContext context, char c, VariableCo
return true;
}

/* array variant */
/* array variantFullText */
if (variableContext.isInsideVariableArray()) {
if (isStringChar(c)) {
context.appendCharToText();
Expand Down

0 comments on commit 4c57e05

Please sign in to comment.