@Override
    public ExitFlags doExit(
        LinkedModeModel environment, VerifyEvent event, int offset, int length) {

      if (event.character == fExitCharacter) {
        if (environment.anyPositionContains(offset)) {
          return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
        } else {
          return new ExitFlags(ILinkedModeListener.UPDATE_CARET, true);
        }
      }

      switch (event.character) {
        case ';':
          return new ExitFlags(ILinkedModeListener.NONE, true);
        case SWT.CR:
          // when entering a function as a parameter, we don't want
          // to jump after the parenthesis when return is pressed
          if (offset > 0) {
            try {
              if (fDocument.getChar(offset - 1) == '{') {
                return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
              }
            } catch (BadLocationException e) {
            }
          }
          return null;
        default:
          return null;
      }
    }
Ejemplo n.º 2
0
  private void ensurePositionCategoryInstalled(final IDocument document, LinkedModeModel model) {
    if (!document.containsPositionCategory(getCategory())) {
      document.addPositionCategory(getCategory());
      fUpdater = new InclusivePositionUpdater(getCategory());
      document.addPositionUpdater(fUpdater);

      model.addLinkingListener(
          new ILinkedModeListener() {

            /*
             * @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.jface.text.link.LinkedModeModel, int)
             */
            @Override
            public void left(LinkedModeModel environment, int flags) {
              ensurePositionCategoryRemoved(document);
            }

            @Override
            public void suspend(LinkedModeModel environment) {}

            @Override
            public void resume(LinkedModeModel environment, int flags) {}
          });
    }
  }
 private boolean isCanceled(IProgressMonitor progressMonitor) {
   return fCanceled
       || progressMonitor.isCanceled()
       || fPostSelectionValidator != null
           && !(fPostSelectionValidator.isValid(fSelection)
               || fForcedMarkOccurrencesSelection == fSelection)
       || LinkedModeModel.hasInstalledModel(fDocument);
 }
Ejemplo n.º 4
0
 private boolean isCanceled(final IProgressMonitor progressMonitor) {
   return fCanceled
       || progressMonitor.isCanceled()
       || fPostSelectionValidator != null
           && !(fPostSelectionValidator.isValid(selection)
               || erlangEditor.markOccurencesHandler.fForcedMarkOccurrencesSelection
                   == selection)
       || LinkedModeModel.hasInstalledModel(fDocument);
 }
  /**
   * Sets up a simple linked mode at {@link #getCursorPosition()} and an exit policy that will exit
   * the mode when <code>closingCharacter</code> is typed and an exit position at <code>
   * getCursorPosition() + 1</code>.
   *
   * @param document the document
   * @param closingCharacter the exit character
   */
  protected void setUpLinkedMode(IDocument document, char closingCharacter) {
    if (getTextViewer() != null && autocloseBrackets()) {
      int offset = getReplacementOffset() + getCursorPosition();
      int exit = getReplacementOffset() + getReplacementString().length();
      try {
        LinkedPositionGroup group = new LinkedPositionGroup();
        group.addPosition(new LinkedPosition(document, offset, 0, LinkedPositionGroup.NO_STOP));

        LinkedModeModel model = new LinkedModeModel();
        model.addGroup(group);
        model.forceInstall();

        LinkedModeUI ui = new EditorLinkedModeUI(model, getTextViewer());
        ui.setSimpleMode(true);
        ui.setExitPolicy(new ExitPolicy(closingCharacter, document));
        ui.setExitPosition(getTextViewer(), exit, 0, Integer.MAX_VALUE);
        ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
        ui.enter();
      } catch (BadLocationException x) {
        DartToolsPlugin.log(x);
      }
    }
  }
Ejemplo n.º 6
0
  /**
   * 執行Quick Fix變更
   *
   * @param activeEditor
   * @param document
   * @throws CoreException
   */
  private void performChange(IEditorPart activeEditor, IDocument document, ASTRewrite rewrite)
      throws CoreException {
    Change change = null;
    IRewriteTarget rewriteTarget = null;
    try {
      change = getChange(actRoot, rewrite);
      if (change != null) {
        if (document != null) {
          LinkedModeModel.closeAllModels(document);
        }
        if (activeEditor != null) {
          rewriteTarget = (IRewriteTarget) activeEditor.getAdapter(IRewriteTarget.class);
          if (rewriteTarget != null) {
            rewriteTarget.beginCompoundChange();
          }
        }

        change.initializeValidationData(new NullProgressMonitor());
        RefactoringStatus valid = change.isValid(new NullProgressMonitor());
        if (valid.hasFatalError()) {
          IStatus status =
              new Status(
                  IStatus.ERROR,
                  JavaPlugin.getPluginId(),
                  IStatus.ERROR,
                  valid.getMessageMatchingSeverity(RefactoringStatus.FATAL),
                  null);
          throw new CoreException(status);
        } else {
          IUndoManager manager = RefactoringCore.getUndoManager();
          manager.aboutToPerformChange(change);
          Change undoChange = change.perform(new NullProgressMonitor());
          manager.changePerformed(change, true);
          if (undoChange != null) {
            undoChange.initializeValidationData(new NullProgressMonitor());
            manager.addUndo("Quick Undo", undoChange);
          }
        }
      }
    } finally {
      if (rewriteTarget != null) {
        rewriteTarget.endCompoundChange();
      }

      if (change != null) {
        change.dispose();
      }
    }
  }
  @Override
  public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
    if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
      LinkedPosition position =
          model.findPosition(new LinkedPosition(fDocument, offset, 0, LinkedPositionGroup.NO_STOP));
      if (position != null) {
        if (event.character == SWT.BS) {
          if (offset - 1 < position.getOffset()) {
            // skip backspace at beginning of linked position
            event.doit = false;
          }
        } else /* event.character == SWT.DEL */ {
          if (offset + 1 > position.getOffset() + position.getLength()) {
            // skip delete at end of linked position
            event.doit = false;
          }
        }
      }
    }

    return null; // don't change behavior
  }
Ejemplo n.º 8
0
  public void start() {
    if (getActiveLinkedMode() != null) {
      // for safety; should already be handled in RenameDartElementAction
      fgActiveLinkedMode.startFullDialog();
      return;
    }

    ISourceViewer viewer = fEditor.getViewer();
    IDocument document = viewer.getDocument();
    fOriginalSelection = viewer.getSelectedRange();
    int offset = fOriginalSelection.x;

    try {
      fLinkedPositionGroup = new LinkedPositionGroup();
      prepareElement();
      if (fDartElement == null) {
        return;
      }

      if (viewer instanceof ITextViewerExtension6) {
        IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager();
        if (undoManager instanceof IUndoManagerExtension) {
          IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager;
          IUndoContext undoContext = undoManagerExtension.getUndoContext();
          IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory();
          fStartingUndoOperation = operationHistory.getUndoOperation(undoContext);
        }
      }

      fOriginalName = nameNode.getName();
      final int pos = nameNode.getOffset();
      final List<ASTNode> sameNodes = Lists.newArrayList();
      nameNode
          .getRoot()
          .accept(
              new RecursiveASTVisitor<Void>() {
                @Override
                public Void visitSimpleIdentifier(SimpleIdentifier node) {
                  Element element = node.getElement();
                  element = getCanonicalElement(element);
                  if (Objects.equal(element, fDartElement)) {
                    sameNodes.add(node);
                  }
                  return super.visitSimpleIdentifier(node);
                }
              });

      // TODO: copied from LinkedNamesAssistProposal#apply(..):
      // sort for iteration order, starting with the node @ offset
      Collections.sort(
          sameNodes,
          new Comparator<ASTNode>() {
            @Override
            public int compare(ASTNode o1, ASTNode o2) {
              return rank(o1) - rank(o2);
            }

            /**
             * Returns the absolute rank of an <code>ASTNode</code>. Nodes preceding <code>pos
             * </code> are ranked last.
             *
             * @param node the node to compute the rank for
             * @return the rank of the node with respect to the invocation offset
             */
            private int rank(ASTNode node) {
              int relativeRank = node.getOffset() + node.getLength() - pos;
              if (relativeRank < 0) {
                return Integer.MAX_VALUE + relativeRank;
              } else {
                return relativeRank;
              }
            }
          });
      for (int i = 0; i < sameNodes.size(); i++) {
        ASTNode elem = sameNodes.get(i);
        LinkedPosition linkedPosition =
            new LinkedPosition(document, elem.getOffset(), elem.getLength(), i);
        if (i == 0) {
          fNamePosition = linkedPosition;
        }
        fLinkedPositionGroup.addPosition(linkedPosition);
      }

      fLinkedModeModel = new LinkedModeModel();
      fLinkedModeModel.addGroup(fLinkedPositionGroup);
      fLinkedModeModel.forceInstall();
      fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor));
      fLinkedModeModel.addLinkingListener(new EditorSynchronizer());

      LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer);
      ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE);
      ui.setExitPolicy(new ExitPolicy(document));
      ui.enter();

      viewer.setSelectedRange(
          fOriginalSelection.x,
          fOriginalSelection.y); // by default, full word is selected; restore original selection

      if (viewer instanceof IEditingSupportRegistry) {
        IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer;
        registry.register(fFocusEditingSupport);
      }

      openSecondaryPopup();
      //			startAnimation();
      fgActiveLinkedMode = this;

    } catch (BadLocationException e) {
      DartToolsPlugin.log(e);
    }
  }
Ejemplo n.º 9
0
 public void cancel() {
   if (fLinkedModeModel != null) {
     fLinkedModeModel.exit(ILinkedModeListener.NONE);
   }
   linkedModeLeft();
 }
Ejemplo n.º 10
0
  @Override
  public void apply(final ITextViewer viewer, char trigger, int stateMask, final int offset) {
    try {
      fLocations = null;
      Point selection = viewer.getSelectedRange();
      final int secectionOffset = selection.x;
      final int selectionLength = selection.y;

      ASTProvider.getASTProvider()
          .runOnAST(
              fTranslationUnit,
              ASTProvider.WAIT_ACTIVE_ONLY,
              new NullProgressMonitor(),
              new ASTRunnable() {
                @Override
                public IStatus runOnAST(ILanguage lang, IASTTranslationUnit astRoot)
                    throws CoreException {
                  if (astRoot == null) return Status.CANCEL_STATUS;

                  IASTNodeSelector selector = astRoot.getNodeSelector(null);
                  IASTName name = selector.findEnclosingName(secectionOffset, selectionLength);
                  if (name != null) {
                    fLocations = LinkedNamesFinder.findByName(astRoot, name);
                  }
                  return Status.OK_STATUS;
                }
              });

      if (fLocations == null || fLocations.length == 0) {
        return;
      }

      // Sort the locations starting with the one @ offset.
      Arrays.sort(
          fLocations,
          new Comparator<IRegion>() {
            @Override
            public int compare(IRegion n1, IRegion n2) {
              return rank(n1) - rank(n2);
            }

            /**
             * Returns the absolute rank of a location. Location preceding {@code offset} are ranked
             * last.
             *
             * @param location the location to compute the rank for
             * @return the rank of the location with respect to the invocation offset
             */
            private int rank(IRegion location) {
              int relativeRank = location.getOffset() + location.getLength() - offset;
              if (relativeRank < 0) {
                return Integer.MAX_VALUE + relativeRank;
              } else {
                return relativeRank;
              }
            }
          });

      IDocument document = viewer.getDocument();
      LinkedPositionGroup group = new LinkedPositionGroup();
      for (int i = 0; i < fLocations.length; i++) {
        IRegion item = fLocations[i];
        group.addPosition(new LinkedPosition(document, item.getOffset(), item.getLength(), i));
      }

      LinkedModeModel model = new LinkedModeModel();
      model.addGroup(group);
      model.forceInstall();
      CEditor editor = getCEditor();
      if (editor != null) {
        model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
      }

      LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
      ui.setExitPolicy(new DeleteBlockingExitPolicy(document));
      ui.setExitPosition(viewer, offset, 0, LinkedPositionGroup.NO_STOP);
      ui.enter();

      if (fValueSuggestion != null) {
        document.replace(fLocations[0].getOffset(), fLocations[0].getLength(), fValueSuggestion);
        IRegion selectedRegion = ui.getSelectedRegion();
        selection = new Point(selectedRegion.getOffset(), fValueSuggestion.length());
      }

      viewer.setSelectedRange(
          selection.x, selection.y); // By default full word is selected, restore original selection
    } catch (BadLocationException e) {
      CUIPlugin.log(e);
    }
  }
  public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    Position[] positions = null;
    FormatterPrefs formatterPrefs = getFormatterPrefs();
    StringBuffer buffer = new StringBuffer();

    buffer.append(fProposal.getName());

    if (formatterPrefs.beforeOpeningParen) buffer.append(SPACE);

    buffer.append(LPAREN);

    if (formatterPrefs.afterOpeningParen) buffer.append(SPACE);

    if (Signature.getParameterCount(fProposal.getSignature()) > 0) {
      char[][] parameters = fProposal.findParameterNames(null);
      if (parameters != null) {
        positions = new Position[parameters.length];

        int base = getReplacementOffset();
        for (int i = 0; i < parameters.length; i++) {
          if (i != 0) {
            if (formatterPrefs.beforeComma) buffer.append(SPACE);

            buffer.append(COMMA);

            if (formatterPrefs.afterComma) buffer.append(SPACE);
          }
          Position position = new Position(0, 0);
          position.setOffset(base + buffer.length());
          position.setLength(parameters[i].length);
          positions[i] = position;
          buffer.append(parameters[i]);
        }
      }
    }

    if (formatterPrefs.beforeClosingParen) buffer.append(SPACE);

    buffer.append(RPAREN);
    setReplacementString(buffer.toString());

    super.apply(viewer, trigger, stateMask, offset);

    try {
      if (positions != null) {
        LinkedModeModel model = new LinkedModeModel();

        for (int i = 0; i < positions.length; i++) {
          LinkedPositionGroup group = new LinkedPositionGroup();
          Position position = positions[i];
          group.addPosition(
              new LinkedPosition(
                  viewer.getDocument(), position.getOffset(), position.getLength(), i));
          model.addGroup(group);
        }
        model.forceInstall();

        LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
        ui.setExitPosition(
            viewer, getReplacementOffset() + getReplacementString().length(), 0, Integer.MAX_VALUE);
        ui.setExitPolicy(new ExitPolicy(')', viewer.getDocument()));
        ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT);
        ui.setDoContextInfo(true);
        ui.enter();
        fSelectedRegion = ui.getSelectedRegion();
      }
    } catch (BadLocationException e) {

    }
  }
Ejemplo n.º 12
0
  public void start() {
    if (getActiveLinkedMode() != null) {
      // for safety; should already be handled in RenameDartElementAction
      fgActiveLinkedMode.startFullDialog();
      return;
    }

    ISourceViewer viewer = fEditor.getViewer();
    IDocument document = viewer.getDocument();
    fOriginalSelection = viewer.getSelectedRange();
    int offset = fOriginalSelection.x;

    try {
      DartUnit root =
          ASTProvider.getASTProvider().getAST(getCompilationUnit(), ASTProvider.WAIT_YES, null);

      fLinkedPositionGroup = new LinkedPositionGroup();
      DartNode selectedNode = NodeFinder.perform(root, fOriginalSelection.x, fOriginalSelection.y);
      if (!(selectedNode instanceof DartIdentifier)) {
        return; // TODO: show dialog
      }
      DartIdentifier nameNode = (DartIdentifier) selectedNode;

      if (viewer instanceof ITextViewerExtension6) {
        IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager();
        if (undoManager instanceof IUndoManagerExtension) {
          IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager;
          IUndoContext undoContext = undoManagerExtension.getUndoContext();
          IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory();
          fStartingUndoOperation = operationHistory.getUndoOperation(undoContext);
        }
      }

      fOriginalName = nameNode.getName();
      final int pos = nameNode.getSourceInfo().getOffset();
      DartNode[] sameNodes = LinkedNodeFinder.findByNode(root, nameNode);

      // TODO: copied from LinkedNamesAssistProposal#apply(..):
      // sort for iteration order, starting with the node @ offset
      Arrays.sort(
          sameNodes,
          new Comparator<DartNode>() {
            @Override
            public int compare(DartNode o1, DartNode o2) {
              return rank(o1) - rank(o2);
            }

            /**
             * Returns the absolute rank of an <code>DartNode</code>. Nodes preceding <code>pos
             * </code> are ranked last.
             *
             * @param node the node to compute the rank for
             * @return the rank of the node with respect to the invocation offset
             */
            private int rank(DartNode node) {
              int relativeRank =
                  node.getSourceInfo().getOffset() + node.getSourceInfo().getLength() - pos;
              if (relativeRank < 0) {
                return Integer.MAX_VALUE + relativeRank;
              } else {
                return relativeRank;
              }
            }
          });
      for (int i = 0; i < sameNodes.length; i++) {
        DartNode elem = sameNodes[i];
        LinkedPosition linkedPosition =
            new LinkedPosition(
                document, elem.getSourceInfo().getOffset(), elem.getSourceInfo().getLength(), i);
        if (i == 0) {
          fNamePosition = linkedPosition;
        }
        fLinkedPositionGroup.addPosition(linkedPosition);
      }

      fLinkedModeModel = new LinkedModeModel();
      fLinkedModeModel.addGroup(fLinkedPositionGroup);
      fLinkedModeModel.forceInstall();
      fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor));
      fLinkedModeModel.addLinkingListener(new EditorSynchronizer());

      LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer);
      ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE);
      ui.setExitPolicy(new ExitPolicy(document));
      ui.enter();

      viewer.setSelectedRange(
          fOriginalSelection.x,
          fOriginalSelection.y); // by default, full word is selected; restore original selection

      if (viewer instanceof IEditingSupportRegistry) {
        IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer;
        registry.register(fFocusEditingSupport);
      }

      openSecondaryPopup();
      //			startAnimation();
      fgActiveLinkedMode = this;

    } catch (BadLocationException e) {
      DartToolsPlugin.log(e);
    }
  }
Ejemplo n.º 13
0
  /** @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent) */
  public void verifyKey(VerifyEvent event) {
    // early pruning to slow down normal typing as little as possible
    if (!event.doit || !isAutoInsertEnabled() || !isAutoInsertCharacter(event.character)) {
      return;
    }

    IDocument document = textViewer.getDocument();
    final Point selection = textViewer.getSelectedRange();
    final int offset = selection.x;
    final int length = selection.y;

    try {

      String scope = getScopeAtOffset(document, offset);
      if (fgCommentSelector.matches(scope)) {
        return;
      }

      if (length > 0) {
        wrapSelection(event, document, offset, length);
        return;
      }

      // Don't auto-close if next char is a letter or digit
      if (document.getLength() > offset) {
        char nextChar = document.getChar(offset);
        if (Character.isJavaIdentifierPart(nextChar)) {
          return;
        }
      }

      // Don't auto-close if we have an open pair!
      if (isUnclosedPair(
          event, document, offset)) // We have an open string or pair, just insert the single
      // character, don't do anything special
      {
        return;
      }

      final char closingCharacter = getPeerCharacter(event.character);
      // If this is the start char and there's no unmatched close char, insert the close char
      if (unpairedClose(event.character, closingCharacter, document, offset)) {
        return;
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append(event.character);
      buffer.append(closingCharacter);
      if (offset == document.getLength()) {
        String delim = null;
        if (document instanceof IDocumentExtension4) {
          delim = ((IDocumentExtension4) document).getDefaultLineDelimiter();
        }
        if (delim == null) {
          delim = System.getProperty("line.separator", "\r\n"); // $NON-NLS-1$ //$NON-NLS-2$
        }
        buffer.append(delim);
      }

      document.replace(offset, length, buffer.toString());

      BracketLevel level = new BracketLevel();
      fBracketLevelStack.push(level);

      LinkedPositionGroup group = new LinkedPositionGroup();
      group.addPosition(new LinkedPosition(document, offset + 1, 0, LinkedPositionGroup.NO_STOP));

      LinkedModeModel model = new LinkedModeModel();
      model.addLinkingListener(this);
      model.addGroup(group);
      model.forceInstall();

      // set up position tracking for our magic peers
      if (fBracketLevelStack.size() == 1) {
        document.addPositionCategory(CATEGORY);
        document.addPositionUpdater(fUpdater);
      }
      level.fFirstPosition = new Position(offset, 1);
      level.fSecondPosition = new Position(offset + 1, 1);
      document.addPosition(CATEGORY, level.fFirstPosition);
      document.addPosition(CATEGORY, level.fSecondPosition);

      level.fUI = new EditorLinkedModeUI(model, textViewer);
      level.fUI.setSimpleMode(true);
      level.fUI.setExitPolicy(
          new ExitPolicy(
              textViewer,
              closingCharacter,
              getEscapeCharacter(closingCharacter),
              fBracketLevelStack));
      level.fUI.setExitPosition(textViewer, offset + 2, 0, Integer.MAX_VALUE);
      level.fUI.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
      level.fUI.enter();

      IRegion newSelection = level.fUI.getSelectedRegion();
      textViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());

      event.doit = false;
    } catch (BadLocationException e) {
      CommonEditorPlugin.logError(e);
    } catch (BadPositionCategoryException e) {
      CommonEditorPlugin.logError(e);
    }
  }
Ejemplo n.º 14
0
  /*
   * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
   */
  @Override
  public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {

    IDocument document = viewer.getDocument();
    try {
      fContext.setReadOnly(false);
      int start;
      TemplateBuffer templateBuffer;
      try {
        beginCompoundChange(viewer);

        int oldReplaceOffset = getReplaceOffset();
        try {
          // this may already modify the document (e.g. add imports)
          templateBuffer = fContext.evaluate(fTemplate);
        } catch (TemplateException e1) {
          fSelectedRegion = fRegion;
          return;
        }

        start = getReplaceOffset();
        int shift = start - oldReplaceOffset;
        int end = Math.max(getReplaceEndOffset(), offset + shift);

        // insert template string
        if (end > document.getLength()) end = offset;
        String templateString = templateBuffer.getString();
        document.replace(start, end - start, templateString);
      } finally {
        endCompoundChange(viewer);
      }

      // translate positions
      LinkedModeModel model = new LinkedModeModel();
      TemplateVariable[] variables = templateBuffer.getVariables();

      MultiVariableGuess guess =
          fContext instanceof CompilationUnitContext
              ? ((CompilationUnitContext) fContext).getMultiVariableGuess()
              : null;

      boolean hasPositions = false;
      for (int i = 0; i != variables.length; i++) {
        TemplateVariable variable = variables[i];

        if (variable.isUnambiguous()) continue;

        LinkedPositionGroup group = new LinkedPositionGroup();

        int[] offsets = variable.getOffsets();
        int length = variable.getLength();

        LinkedPosition first;
        if (guess != null && variable instanceof MultiVariable) {
          first =
              new VariablePosition(
                  document, offsets[0] + start, length, guess, (MultiVariable) variable);
          guess.addSlave((VariablePosition) first);
        } else {
          String[] values = variable.getValues();
          ICompletionProposal[] proposals = new ICompletionProposal[values.length];
          for (int j = 0; j < values.length; j++) {
            ensurePositionCategoryInstalled(document, model);
            Position pos = new Position(offsets[0] + start, length);
            document.addPosition(getCategory(), pos);
            proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
          }

          if (proposals.length > 1)
            first = new ProposalPosition(document, offsets[0] + start, length, proposals);
          else first = new LinkedPosition(document, offsets[0] + start, length);
        }

        for (int j = 0; j != offsets.length; j++)
          if (j == 0) group.addPosition(first);
          else group.addPosition(new LinkedPosition(document, offsets[j] + start, length));

        model.addGroup(group);
        hasPositions = true;
      }

      if (hasPositions) {
        model.forceInstall();
        JavaEditor editor = getJavaEditor();
        if (editor != null) {
          model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
        }

        LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
        ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
        ui.enter();

        fSelectedRegion = ui.getSelectedRegion();
      } else {
        fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
      }

    } catch (BadLocationException e) {
      JavaPlugin.log(e);
      openErrorDialog(viewer.getTextWidget().getShell(), e);
      fSelectedRegion = fRegion;
    } catch (BadPositionCategoryException e) {
      JavaPlugin.log(e);
      openErrorDialog(viewer.getTextWidget().getShell(), e);
      fSelectedRegion = fRegion;
    }
  }