/** * 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) { JavaPlugin.log(x); } } }
private void startLinkedEdit(List<IRegion> selections, ITextViewer viewer, Point originalSelection) throws BadLocationException { final LinkedPositionGroup linkedPositionGroup = new LinkedPositionGroup(); for (IRegion selection : selections) { linkedPositionGroup.addPosition(new LinkedPosition(viewer.getDocument(), selection.getOffset(), selection .getLength())); } LinkedModeModel model = new LinkedModeModel(); model.addGroup(linkedPositionGroup); model.forceInstall(); //FIXME can add a listener here to listen for the end of linked mode //model.addLinkingListener(null); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPolicy(new DeleteBlockingExitPolicy(viewer.getDocument())); ui.enter(); // by default the text being edited is selected so restore original selection viewer.setSelectedRange(originalSelection.x, originalSelection.y); }
protected void applyLinkedMode(ITextViewer viewer) throws BadLocationException { LinkedModeModel model = getLinkedModeModel(viewer); if(model == null) { return; } model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPolicy(new CompletionProposalExitPolicy()); ui.setExitPosition(viewer, endPositionAfterApply, 0, Integer.MAX_VALUE); if(firstLinkedModeGroupPosition != -1) { positionAfterApply = null; } ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT); ui.setDoContextInfo(true); ui.enableColoredLabels(true); ui.enter(); }
public void apply(IDocument document) { try { if (fentry.arguments > 0) { StringBuffer displayKey = new StringBuffer(fentry.key); for (int j=0; j < fentry.arguments; j++) displayKey.append("{}"); document.replace(fReplacementOffset, fReplacementLength, displayKey.toString()); if (TexlipsePlugin.getDefault().getPreferenceStore() .getBoolean(TexlipseProperties.SMART_PARENS)){ LinkedModeModel model= new LinkedModeModel(); for (int j=0; j < fentry.arguments; j++){ int newOffset = fReplacementOffset + fentry.key.length() + j*2 + 1; LinkedPositionGroup group = new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, newOffset, 0, LinkedPositionGroup.NO_STOP)); model.addGroup(group); } model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, fviewer); ui.setSimpleMode(false); ui.setExitPolicy(new ExitPolicy('}', fviewer)); ui.setExitPosition(fviewer, fReplacementOffset + displayKey.length(), 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER); ui.enter(); } } else { document.replace(fReplacementOffset, fReplacementLength, fentry.key); } } catch (BadLocationException x) { } }
public boolean start(IRenameElementContext renameElementContext, IProgressMonitor monitor) { if (renameElementContext == null) throw new IllegalArgumentException("RenameElementContext is null"); this.linkedPositionGroup = linkedPositionGroupCalculator.getLinkedPositionGroup(renameElementContext, monitor); if (linkedPositionGroup == null || linkedPositionGroup.isEmpty()) return false; this.editor = (XtextEditor) renameElementContext.getTriggeringEditor(); this.focusEditingSupport = new FocusEditingSupport(); ISourceViewer viewer = editor.getInternalSourceViewer(); IDocument document = viewer.getDocument(); originalSelection = viewer.getSelectedRange(); currentPosition = linkedPositionGroup.getPositions()[0]; originalName = getCurrentName(); try { linkedModeModel = new LinkedModeModel(); linkedModeModel.addGroup(linkedPositionGroup); linkedModeModel.forceInstall(); linkedModeModel.addLinkingListener(new EditorSynchronizer()); LinkedModeUI ui = new EditorLinkedModeUI(linkedModeModel, viewer); ui.setExitPolicy(new ExitPolicy(document)); if (currentPosition.includes(originalSelection.x)) ui.setExitPosition(viewer, originalSelection.x, 0, Integer.MAX_VALUE); ui.enter(); if (currentPosition.includes(originalSelection.x) && currentPosition.includes(originalSelection.x + originalSelection.y)) viewer.setSelectedRange(originalSelection.x, originalSelection.y); if (viewer instanceof IEditingSupportRegistry) { IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer; registry.register(focusEditingSupport); } openPopup(); return true; } catch (BadLocationException e) { throw new WrappedException(e); } }
@Override public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { apply(viewer.getDocument()); try { LinkedModeModel model = new LinkedModeModel(); StringBuffer insert = new StringBuffer(); insert.append(macro.name); insert.append("("); int parameterOffset = position.offset + insert.length(); for (int k = 0; k < macro.parameters.length; k++) { LinkedPositionGroup group = new LinkedPositionGroup(); if (k > 0) // space between parameters parameterOffset++; group.addPosition(new LinkedPosition(viewer.getDocument(), parameterOffset, macro.parameters[k].length(), LinkedPositionGroup.NO_STOP)); model.addGroup(group); parameterOffset += macro.parameters[k].length(); } model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPosition(viewer, parameterOffset + 1, 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_ALWAYS); ui.enter(); fSelectedRegion = ui.getSelectedRegion(); } catch (Exception e) { e.printStackTrace(); } }
@Override public void apply(IDocument document, char trigger, int offset) { super.apply(document, trigger, offset); int baseOffset= getReplacementOffset(); String replacement= getReplacementString(); if (fArgumentOffsets != null && getTextViewer() != null) { try { LinkedModeModel model= new LinkedModeModel(); for (int i= 0; i != fArgumentOffsets.length; i++) { LinkedPositionGroup group= new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, baseOffset + fArgumentOffsets[i], fArgumentLengths[i], LinkedPositionGroup.NO_STOP)); model.addGroup(group); } model.forceInstall(); JavaEditor editor= getJavaEditor(); if (editor != null) { model.addLinkingListener(new EditorHighlightingSynchronizer(editor)); } LinkedModeUI ui= new EditorLinkedModeUI(model, getTextViewer()); ui.setExitPosition(getTextViewer(), baseOffset + replacement.length(), 0, Integer.MAX_VALUE); ui.setExitPolicy(new ExitPolicy(')', document)); ui.setDoContextInfo(true); ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT); ui.enter(); fSelectedRegion= ui.getSelectedRegion(); } catch (BadLocationException e) { JavaPlugin.log(e); openErrorDialog(e); } } else { fSelectedRegion= new Region(baseOffset + replacement.length(), 0); } }
private void goToLinkedMode(ITextViewer viewer, int offset, IDocument doc, int exitPos, int iPar, List<Integer> offsetsAndLens) throws BadLocationException { if (!goToLinkedMode) { return; } if (offsetsAndLens.size() > 0) { LinkedModeModel model = new LinkedModeModel(); for (int i = 0; i < offsetsAndLens.size(); i++) { Integer offs = offsetsAndLens.get(i); i++; Integer len = offsetsAndLens.get(i); if (i == 1) { firstParameterLen = len; } int location = offset + iPar + offs + 1; LinkedPositionGroup group = new LinkedPositionGroup(); ProposalPosition proposalPosition = new ProposalPosition(doc, location, len, 0, new ICompletionProposal[0]); group.addPosition(proposalPosition); model.addGroup(group); } model.forceInstall(); final LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setDoContextInfo(true); //set it to request the ctx info from the completion processor ui.setExitPosition(viewer, exitPos, 0, Integer.MAX_VALUE); Runnable r = new Runnable() { @Override public void run() { ui.enter(); } }; RunInUiThread.async(r); } }
public void start() { if (getActiveLinkedMode() != null) { // for safety; should already be handled in RenameJavaElementAction fgActiveLinkedMode.startFullDialog(); return; } ISourceViewer viewer = fEditor.getViewer(); IDocument document = viewer.getDocument(); ITypeScriptFile tsFile = fEditor.getTypeScriptFile(); tsFile.setDisableChanged(true); fOriginalSelection = viewer.getSelectedRange(); int offset = fOriginalSelection.x; try { fLinkedPositionGroup = new LinkedPositionGroup(); 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); } } // Find occurrences List<OccurrencesResponseItem> occurrences = tsFile.occurrences(offset).get(1000, TimeUnit.MILLISECONDS); // Create Eclipse linked position from the occurrences list. int start, length; for (int i = 0; i < occurrences.size(); i++) { OccurrencesResponseItem item = occurrences.get(i); start = tsFile.getPosition(item.getStart()); length = tsFile.getPosition(item.getEnd()) - start; LinkedPosition linkedPosition = new LinkedPosition(document, start, length, i); if (i == 0) { fOriginalName = document.get(start, length); 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 (Exception e) { JSDTTypeScriptUIPlugin.log(e); } }
/** * Sets up linked positions and installs them on the viewer. * */ protected void getLinkedPositions(ITextViewer viewer) { final String replacement = getReplacementString(); final IDocument document = viewer.getDocument(); final int length = replacement.length(); boolean inAttribute = false, hasGroup = false; int offset = 0; char attType = 0; int exitPosition = -1; LinkedModeModel model = new LinkedModeModel(); try { for (int i = 0; i < length; i++) { final char c = replacement.charAt(i); switch (c) { case '=': break; case '\'': case '\"': if (!inAttribute) { offset = i; attType = c; inAttribute = true; } else { // Found matching quotes establishing an attribute value // region if (attType == c && replacement.charAt(i - 1) != '\\') { inAttribute = false; // Record position length addPosition(model, document, getReplacementOffset() + offset + 1, i - offset - 1); hasGroup = true; } } break; case '>': if (!inAttribute && exitPosition == -1) { exitPosition = getReplacementOffset() + i + 1; } break; } } if (hasGroup) { model.forceInstall(); final LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPosition(viewer, exitPosition < 0 ? getReplacementOffset() + getReplacementLength() + replacement.length() - 1 : exitPosition, 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT); ui.setDoContextInfo(true); ui.enter(); fSelectedRegion = ui.getSelectedRegion(); } } catch (BadLocationException e) { Logger.logException(e); } }
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { try { Point seletion= viewer.getSelectedRange(); // get full ast CompilationUnit root= SharedASTProvider.getAST(fContext.getCompilationUnit(), SharedASTProvider.WAIT_YES, null); ASTNode nameNode= NodeFinder.perform(root, fNode.getStartPosition(), fNode.getLength()); final int pos= fNode.getStartPosition(); ASTNode[] sameNodes; if (nameNode instanceof SimpleName) { sameNodes= LinkedNodeFinder.findByNode(root, (SimpleName) nameNode); } else { sameNodes= new ASTNode[] { nameNode }; } // sort for iteration order, starting with the node @ offset Arrays.sort(sameNodes, new Comparator<ASTNode>() { public int compare(ASTNode o1, ASTNode o2) { return rank(o1) - rank(o2); } /** * Returns the absolute rank of an <code>ASTNode</code>. Nodes * preceding <code>offset</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.getStartPosition() + node.getLength() - pos; if (relativeRank < 0) return Integer.MAX_VALUE + relativeRank; else return relativeRank; } }); IDocument document= viewer.getDocument(); LinkedPositionGroup group= new LinkedPositionGroup(); for (int i= 0; i < sameNodes.length; i++) { ASTNode elem= sameNodes[i]; group.addPosition(new LinkedPosition(document, elem.getStartPosition(), elem.getLength(), i)); } LinkedModeModel model= new LinkedModeModel(); model.addGroup(group); model.forceInstall(); if (fContext instanceof AssistContext) { IEditorPart editor= ((AssistContext)fContext).getEditor(); if (editor instanceof JavaEditor) { model.addLinkingListener(new EditorHighlightingSynchronizer((JavaEditor) 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(nameNode.getStartPosition(), nameNode.getLength(), fValueSuggestion); IRegion selectedRegion= ui.getSelectedRegion(); seletion= new Point(selectedRegion.getOffset(), fValueSuggestion.length()); } viewer.setSelectedRange(seletion.x, seletion.y); // by default full word is selected, restore original selection } catch (BadLocationException e) { JavaPlugin.log(e); } }
@Override public void apply(IDocument document) { try { //default apply int lineOfOffset = document.getLineOfOffset(fReplacementOffset); document.replace(fReplacementOffset, fReplacementLength, fReplacementString); if (SharedCorePlugin.inTestMode()) { return; } int lineOffset = document.getLineOffset(lineOfOffset); int lineLength = document.getLineLength(lineOfOffset); String lineDelimiter = document.getLineDelimiter(lineOfOffset); int lineDelimiterLen = lineDelimiter != null ? lineDelimiter.length() : 0; ISourceViewer viewer = sourceViewer; LinkedModeModel model = new LinkedModeModel(); LinkedPositionGroup group = new LinkedPositionGroup(); //the len-3 is because of the end of the string: " = " because the replacement string is //something like "xxx = " ProposalPosition proposalPosition = new ProposalPosition(document, fReplacementOffset, fReplacementString.length() - 3, 0, new ICompletionProposal[0]); group.addPosition(proposalPosition); model.addGroup(group); model.forceInstall(); final LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPosition(viewer, lineOffset + lineLength - lineDelimiterLen, 0, Integer.MAX_VALUE); Runnable r = new Runnable() { @Override public void run() { ui.enter(); } }; RunInUiThread.async(r); } catch (Throwable x) { // ignore Log.log(x); } }
/** * Mostly based on code from {@link org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal} */ private void startEditing(ISourceViewer viewer) throws ExecutionException { Point selOffsetAndLen = viewer.getSelectedRange(); int selStart = CoordinatesUtil.fromOffsetAndLengthToStartAndEnd(selOffsetAndLen).x; IDocument document = viewer.getDocument(); try { String selectedText; if (selOffsetAndLen.y == 0) { // no characters selected String documentText = document.get(); Point wordOffsetAndLen = TextUtil.findWordSurrounding(documentText, selStart); if (wordOffsetAndLen != null) { selectedText = document.get(wordOffsetAndLen.x, wordOffsetAndLen.y); } else { IRegion selectedLine = document.getLineInformationOfOffset(selStart); selectedText = document.get(selectedLine.getOffset(), selectedLine.getLength()); } } else { selectedText = document.get(selOffsetAndLen.x, selOffsetAndLen.y); } LinkedPositionGroup linkedPositionGroup = new LinkedPositionGroup(); FindReplaceDocumentAdapter findReplaceAdaptor = new FindReplaceDocumentAdapter(document); IRegion matchingRegion = findReplaceAdaptor.find(0, selectedText, true, true, false, false); while (matchingRegion != null) { linkedPositionGroup.addPosition(new LinkedPosition(document, matchingRegion.getOffset(), matchingRegion .getLength())); matchingRegion = findReplaceAdaptor.find(matchingRegion.getOffset() + matchingRegion.getLength(), selectedText, true, true, false, false); } LinkedModeModel model = new LinkedModeModel(); model.addGroup(linkedPositionGroup); model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPolicy(new DeleteBlockingExitPolicy(document)); ui.enter(); // by default the text being edited is selected so restore original selection viewer.setSelectedRange(selOffsetAndLen.x, selOffsetAndLen.y); } catch (BadLocationException e) { throw new ExecutionException("Editing failed", e); } }