VariableReplacementRegionCalculator implements IVariableReplacementRegionCalculator { @Override public IRegion calculate(ParsedString argument, int cursorOffsetInDocument) { String arg = argument.getValue(); int cursorOffset = cursorOffsetInDocument - argument.getArgCharPos(); if (cursorOffset == 0) { return new Region(cursorOffsetInDocument, 0); } int startPos = getStartPos(arg, cursorOffset); if (startPos == -1) { return new Region(cursorOffsetInDocument, 0); } int startPosInDocument = argument.getArgCharPos() + startPos; if (!isCharAt(arg, startPos + 1, '{')) { if (cursorOffset == startPos + 1) { return new Region(startPosInDocument, 1); } return new Region(cursorOffsetInDocument, 0); } int closePos = arg.indexOf('}', startPos + 2); if (closePos != -1 && closePos < cursorOffset) { return new Region(cursorOffsetInDocument, 0); } int nextDollarPos = arg.indexOf('$', startPos + 2); int nextAtPos = arg.indexOf('@', startPos + 2); if (closePos == -1 && nextDollarPos == -1 && nextAtPos == -1) { return new Region(startPosInDocument, argument.getArgEndCharPos() - startPosInDocument); } int endPos = getEndPos(closePos, nextDollarPos, nextAtPos); boolean cursorAtVariableStart = cursorOffset == startPos + 1 || cursorOffset == startPos + 2; boolean isClosedVariable = closePos == endPos - 1; if (cursorAtVariableStart && !isClosedVariable) { return new Region(startPosInDocument, 2); } return new Region(startPosInDocument, endPos - startPos); }  @Override IRegion calculate(ParsedString argument, int cursorOffsetInDocument);  }
@Test public void partialVariableIsReplacedWhenCursonIsInsidePartialVariable() { IRegion region = c.calculate(ARGUMENT, INSIDE_PARTIAL_VARIABLE_POS); assertEquals(PARTIAL_VARIABLE_START_POS, region.getOffset()); assertEquals(PARTIAL_VARIABLE_LENGTH, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursorIsBeforeVariable() { IRegion region = c.calculate(ARGUMENT, COMPLETE_VARIABLE_START_POS); assertEquals(COMPLETE_VARIABLE_START_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursorIsAfterVariable() { IRegion region = c.calculate(ARGUMENT, COMPLETE_VARIABLE_START_POS + COMPLETE_VARIABLE_LENGTH); assertEquals(COMPLETE_VARIABLE_START_POS + COMPLETE_VARIABLE_LENGTH, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursorIsInsideTextAfterClosedVariable() { IRegion region = c.calculate(ARGUMENT_2, AFTER_CLOSED_VARIABLE_POS); assertEquals(AFTER_CLOSED_VARIABLE_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void variableStartIsReplacedWhenCursorIsInPartialVariableStart() { IRegion region = c.calculate(ARGUMENT, PARTIAL_VARIABLE_START_POS + 1); assertEquals(PARTIAL_VARIABLE_START_POS, region.getOffset()); assertEquals(2, region.getLength()); }
@Test public void variableStartIsReplacedWhenCursorIsAfterPartialVariableStart() { IRegion region = c.calculate(ARGUMENT, PARTIAL_VARIABLE_START_POS + 2); assertEquals(PARTIAL_VARIABLE_START_POS, region.getOffset()); assertEquals(2, region.getLength()); }
@Test public void variableIsReplacedWhenCursorIsInCompleteVariableStart() { IRegion region = c.calculate(ARGUMENT, COMPLETE_VARIABLE_START_POS + 1); assertEquals(COMPLETE_VARIABLE_START_POS, region.getOffset()); assertEquals(COMPLETE_VARIABLE_LENGTH, region.getLength()); }
@Test public void variableIsReplacedWhenCursorIsAfterCompleteVariableStart() { IRegion region = c.calculate(ARGUMENT, COMPLETE_VARIABLE_START_POS + 2); assertEquals(COMPLETE_VARIABLE_START_POS, region.getOffset()); assertEquals(COMPLETE_VARIABLE_LENGTH, region.getLength()); }
@Test public void doesNotReplaceAnythingForEmptyArgument() { IRegion region = c.calculate(new ParsedString("", ARG_START_POS), ARG_START_POS); assertEquals(ARG_START_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenAtBeginningOfArgument() { IRegion region = c.calculate(ARGUMENT, ARG_START_POS); assertEquals(ARG_START_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenThereIsNotVariableInArgument() { IRegion region = c.calculate(new ParsedString("foobar{foobar}", ARG_START_POS), ARG_START_POS + 8); assertEquals(ARG_START_POS + 8, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void partialVariableIsReplacedWhenCursonIsAfterPartialVariable() { IRegion region = c.calculate(ARGUMENT, PARTIAL_VARIABLE_START_POS + PARTIAL_VARIABLE_LENGTH); assertEquals(PARTIAL_VARIABLE_START_POS, region.getOffset()); assertEquals(PARTIAL_VARIABLE_LENGTH, region.getLength()); }
@Test public void replacesStartCharacterBeforeCursorIfNotFollowedByCurlyBrace() { IRegion region = c.calculate(new ParsedString("foobar$foobar", ARG_START_POS), ARG_START_POS + 7); assertEquals(ARG_START_POS + 6, region.getOffset()); assertEquals(1, region.getLength()); }
@Test public void replacesStartCharacterBeforeCursorIfItIsTheLastCharacter() { IRegion region = c.calculate(new ParsedString("foobar$", ARG_START_POS), ARG_START_POS + 7); assertEquals(ARG_START_POS + 6, region.getOffset()); assertEquals(1, region.getLength()); }
@Test public void replacesStartCharacterBeforeCursorIfItIsTheOnlyCharacter() { IRegion region = c.calculate(new ParsedString("@", ARG_START_POS), ARG_START_POS + 1); assertEquals(ARG_START_POS, region.getOffset()); assertEquals(1, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursorIsBeforePartialVariable() { IRegion region = c.calculate(ARGUMENT, PARTIAL_VARIABLE_START_POS); assertEquals(PARTIAL_VARIABLE_START_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void partialVariableAtTheEndIsReplacedWhenCursonIsInsidePartialVariable() { IRegion region = c.calculate(PARTIAL_VARIABLE_AT_THE_END, INSIDE_PARTIAL_VARIABLE_AT_THE_END_POS); assertEquals(PARTIAL_VARIABLE_AT_THE_END_START_POS, region.getOffset()); assertEquals(PARTIAL_VARIABLE_AT_THE_END_LENGTH, region.getLength()); }
@Test public void partialVariableAtTheEndIsReplacedWhenCursonIsAfterPartialVariable() { IRegion region = c.calculate(PARTIAL_VARIABLE_AT_THE_END, PARTIAL_VARIABLE_AT_THE_END_START_POS + PARTIAL_VARIABLE_AT_THE_END_LENGTH); assertEquals(PARTIAL_VARIABLE_AT_THE_END_START_POS, region.getOffset()); assertEquals(PARTIAL_VARIABLE_AT_THE_END_LENGTH, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursorIsBeforePartialVariableAtTheEnd() { IRegion region = c.calculate(PARTIAL_VARIABLE_AT_THE_END, PARTIAL_VARIABLE_AT_THE_END_START_POS); assertEquals(PARTIAL_VARIABLE_AT_THE_END_START_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void completeVariableAtTheEndIsReplacedWhenCursonIsInsideCompleteVariable() { IRegion region = c.calculate(COMPLETE_VARIABLE_AT_THE_END, INSIDE_COMPLETE_VARIABLE_AT_THE_END_POS); assertEquals(COMPLETE_VARIABLE_AT_THE_END_START_POS, region.getOffset()); assertEquals(COMPLETE_VARIABLE_AT_THE_END_LENGTH, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursonIsAfterCompleteVariableAtTheEnd() { IRegion region = c.calculate(COMPLETE_VARIABLE_AT_THE_END, COMPLETE_VARIABLE_AT_THE_END_START_POS + COMPLETE_VARIABLE_AT_THE_END_LENGTH); assertEquals(COMPLETE_VARIABLE_AT_THE_END_START_POS + COMPLETE_VARIABLE_AT_THE_END_LENGTH, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void doesNotReplaceAnythingWhenCursorIsBeforeCompleteVariableAtTheEnd() { IRegion region = c.calculate(COMPLETE_VARIABLE_AT_THE_END, COMPLETE_VARIABLE_AT_THE_END_START_POS); assertEquals(COMPLETE_VARIABLE_AT_THE_END_START_POS, region.getOffset()); assertEquals(0, region.getLength()); }
@Test public void completeVariableIsReplacedWhenCursorIsInsideVariable() { IRegion region = c.calculate(ARGUMENT, INSIDE_COMPLETE_VARIABLE_POS); assertEquals(COMPLETE_VARIABLE_START_POS, region.getOffset()); assertEquals(COMPLETE_VARIABLE_LENGTH, region.getLength()); }