org.eclipse.jface.text.link.LinkedPosition Java Examples

The following examples show how to use org.eclipse.jface.text.link.LinkedPosition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LangCompletionProposal.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
protected LinkedModeModel getLinkedModeModel(ITextViewer viewer) throws BadLocationException {
	Indexable<SourceRange> sourceSubElements = proposal.getSourceSubElements();
	if(sourceSubElements == null || sourceSubElements.isEmpty()) {
		return null;
	}
	
	LinkedModeModel model = new LinkedModeModel();
	
	IDocument document = viewer.getDocument();
	int replaceOffset = getReplaceOffset();
	
	firstLinkedModeGroupPosition = -1;
	
	for (SourceRange sr : sourceSubElements) {
		LinkedPositionGroup group = new LinkedPositionGroup();
		int posOffset = replaceOffset + sr.getOffset();
		group.addPosition(new LinkedPosition(document, posOffset, sr.getLength()));
		if(firstLinkedModeGroupPosition == -1) {
			firstLinkedModeGroupPosition = posOffset;
		}
		model.addGroup(group);
	}
	
	return model;
}
 
Example #2
Source File: LinkedNamesAssistProposal.java    From typescript.java with MIT License 6 votes vote down vote up
@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
}
 
Example #3
Source File: DefaultLinkedPositionGroupCalculator.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected Iterable<LinkedPosition> sortPositions(Iterable<LinkedPosition> linkedPositions,
		final int invocationOffset) {
	Comparator<LinkedPosition> comparator = new Comparator<LinkedPosition>() {

		@Override
		public int compare(LinkedPosition left, LinkedPosition right) {
			return rank(left) - rank(right);
		}

		private int rank(LinkedPosition o1) {
			int relativeRank = o1.getOffset() + o1.length - invocationOffset;
			if (relativeRank < 0)
				return Integer.MAX_VALUE + relativeRank;
			else
				return relativeRank;
		}
	};
	return ImmutableSortedSet.copyOf(comparator, linkedPositions);
}
 
Example #4
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example #5
Source File: RenameRefactoringPopup.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected Point computePopupLocation() {
	if (popup == null || popup.isDisposed())
		return null;

	LinkedPosition position = renameLinkedMode.getCurrentLinkedPosition();
	if (position == null)
		return null;
	ISourceViewer viewer = editor.getInternalSourceViewer();
	ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer;
	int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset);

	StyledText textWidget = viewer.getTextWidget();
	Point pos = textWidget.getLocationAtOffset(widgetOffset);
	Point pSize = getExtent();
	pSize.y += HAH + 1;
	pos.x -= HAO;
	pos.y += textWidget.getLineHeight(widgetOffset);
	Point dPos = textWidget.toDisplay(pos);
	Rectangle displayBounds = textWidget.getDisplay().getClientArea();
	Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize);
	Geometry.moveInside(dPopupRect, displayBounds);
	return new Point(dPopupRect.x, dPopupRect.y);
}
 
Example #6
Source File: RenameLinkedMode.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
	showPreview = (event.stateMask & SWT.CTRL) != 0 && (event.character == SWT.CR || event.character == SWT.LF);
	if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
		LinkedPosition position = model.findPosition(new LinkedPosition(document, 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
}
 
Example #7
Source File: LinkedNamesAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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
}
 
Example #8
Source File: ConfigurableCompletionProposal.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
	 * 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
	 */
	protected void setUpLinkedMode(IDocument document) {
		try {
			LinkedPositionGroup group= new LinkedPositionGroup();
			group.addPosition(new LinkedPosition(document, getSelectionStart(), getSelectionLength(), LinkedPositionGroup.NO_STOP));

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

			LinkedModeUI ui= new LinkedModeUI(model, viewer);
//			ui.setSimpleMode(true);
			ui.setExitPolicy(new ExitPolicy(exitChars));
			ui.setExitPosition(viewer, getCursorPosition() + getReplacementOffset(), 0, Integer.MAX_VALUE);
			ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
			ui.enter();
		} catch (BadLocationException e) {
			log.info(e.getMessage(), e);
		}
	}
 
Example #9
Source File: DefaultLinkedPositionGroupCalculator2.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected Iterable<LinkedPosition> sortPositions(Iterable<LinkedPosition> linkedPositions, int invocationOffset) {
	Comparator<LinkedPosition> comparator = new Comparator<LinkedPosition>() {

		@Override
		public int compare(LinkedPosition left, LinkedPosition right) {
			return rank(left) - rank(right);
		}

		private int rank(LinkedPosition o1) {
			int relativeRank = (o1.offset + o1.length) - invocationOffset;
			if (relativeRank < 0) {
				return Integer.MAX_VALUE + relativeRank;
			} else {
				return relativeRank;
			}
		}
	};
	return ImmutableSortedSet.copyOf(comparator, linkedPositions);
}
 
Example #10
Source File: DeleteBlockingExitPolicy.java    From eclipse-multicursor with Eclipse Public License 1.0 6 votes vote down vote up
@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
}
 
Example #11
Source File: SelectNextOccurrenceHandler.java    From eclipse-multicursor with Eclipse Public License 1.0 6 votes vote down vote up
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);
}
 
Example #12
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the next position after the given position.
 *
 * @param position the current position
 * @return the next position
 */
protected int findNextPosition(int position) {
    ISourceViewer viewer = getSourceViewer();
    int widget = -1;
    int next = position;
    while (next != BreakIterator.DONE && widget == -1) { // XXX: optimize
        next = fIterator.following(next);
        if (next != BreakIterator.DONE) {
            widget = modelOffset2WidgetOffset(viewer, next);
        }
    }

    IDocument document = viewer.getDocument();
    LinkedModeModel model = LinkedModeModel.getModel(document, position);
    if (model != null && next != BreakIterator.DONE) {
        LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0));
        if (linkedPosition != null) {
            int linkedPositionEnd = linkedPosition.getOffset() + linkedPosition.getLength();
            if (position != linkedPositionEnd && linkedPositionEnd < next) {
                next = linkedPositionEnd;
            }
        } else {
            LinkedPosition nextLinkedPosition = model.findPosition(new LinkedPosition(document, next, 0));
            if (nextLinkedPosition != null) {
                int nextLinkedPositionOffset = nextLinkedPosition.getOffset();
                if (position != nextLinkedPositionOffset && nextLinkedPositionOffset < next) {
                    next = nextLinkedPositionOffset;
                }
            }
        }
    }

    return next;
}
 
Example #13
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the previous position before the given position.
 *
 * @param position the current position
 * @return the previous position
 */
protected int findPreviousPosition(int position) {
    ISourceViewer viewer = getSourceViewer();
    int widget = -1;
    int previous = position;
    while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize
        previous = fIterator.preceding(previous);
        if (previous != BreakIterator.DONE) {
            widget = modelOffset2WidgetOffset(viewer, previous);
        }
    }

    IDocument document = viewer.getDocument();
    LinkedModeModel model = LinkedModeModel.getModel(document, position);
    if (model != null && previous != BreakIterator.DONE) {
        LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0));
        if (linkedPosition != null) {
            int linkedPositionOffset = linkedPosition.getOffset();
            if (position != linkedPositionOffset && previous < linkedPositionOffset) {
                previous = linkedPositionOffset;
            }
        } else {
            LinkedPosition previousLinkedPosition = model
                    .findPosition(new LinkedPosition(document, previous, 0));
            if (previousLinkedPosition != null) {
                int previousLinkedPositionEnd = previousLinkedPosition.getOffset()
                        + previousLinkedPosition.getLength();
                if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd) {
                    previous = previousLinkedPositionEnd;
                }
            }
        }
    }

    return previous;
}
 
Example #14
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the previous position before the given position.
 *
 * @param position the current position
 * @return the previous position
 */
protected int findPreviousPosition(int position) {
	ISourceViewer viewer= getSourceViewer();
	int widget= -1;
	int previous= position;
	while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize
		previous= fIterator.preceding(previous);
		if (previous != BreakIterator.DONE)
			widget= modelOffset2WidgetOffset(viewer, previous);
	}

	IDocument document= viewer.getDocument();
	LinkedModeModel model= LinkedModeModel.getModel(document, position);
	if (model != null && previous != BreakIterator.DONE) {
		LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, position, 0));
		if (linkedPosition != null) {
			int linkedPositionOffset= linkedPosition.getOffset();
			if (position != linkedPositionOffset && previous < linkedPositionOffset)
				previous= linkedPositionOffset;
		} else {
			LinkedPosition previousLinkedPosition= model.findPosition(new LinkedPosition(document, previous, 0));
			if (previousLinkedPosition != null) {
				int previousLinkedPositionEnd= previousLinkedPosition.getOffset() + previousLinkedPosition.getLength();
				if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd)
					previous= previousLinkedPositionEnd;
			}
		}
	}

	return previous;
}
 
Example #15
Source File: LinkedModelCalculatorIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCorrectTextEditsDeclarationInFile() throws Exception {
	String initialModel1 = "A { B ref A } C { D ref A }";
	createFile(pathToFile1, initialModel1);
	String initialModel2 = "E { ref A } F { ref E ref A}";
	IFile file1 = createFile(pathToFile1, initialModel1);
	createFile(pathToFile2, initialModel2);
	waitForBuild();
	XtextEditor editor = openEditor(file1);
	EObject a = editor.getDocument().readOnly(new IUnitOfWork<EObject, XtextResource>() {
		@Override
		public EObject exec(XtextResource state) throws Exception {
			return state.getContents().get(0).eContents().get(0);
		}
	});
	URI uri = EcoreUtil.getURI(a);
	selectElementInEditor(a, uri, editor, (XtextResource) a.eResource());
	IRenameElementContext renameElementContext = new IRenameElementContext.Impl(uri, a.eClass(), editor, editor
			.getSelectionProvider().getSelection(), uriToFile1);
	LinkedPositionGroup linkedPositionGroup = linkedModelCalculator.getLinkedPositionGroup(renameElementContext,
			new NullProgressMonitor()).get();
	LinkedPosition[] positions = linkedPositionGroup.getPositions();
	assertEquals(3, positions.length);
	int[] offsets = { 0, 10, 24 };
	for (int i = 0; i < positions.length; i++) {
		assertEquals(offsets[i], positions[i].getOffset());
	}
}
 
Example #16
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the next position after the given position.
 *
 * @param position the current position
 * @return the next position
 */
protected int findNextPosition(int position) {
	ISourceViewer viewer= getSourceViewer();
	int widget= -1;
	int next= position;
	while (next != BreakIterator.DONE && widget == -1) { // XXX: optimize
		next= fIterator.following(next);
		if (next != BreakIterator.DONE)
			widget= modelOffset2WidgetOffset(viewer, next);
	}

	IDocument document= viewer.getDocument();
	LinkedModeModel model= LinkedModeModel.getModel(document, position);
	if (model != null && next != BreakIterator.DONE) {
		LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, position, 0));
		if (linkedPosition != null) {
			int linkedPositionEnd= linkedPosition.getOffset() + linkedPosition.getLength();
			if (position != linkedPositionEnd && linkedPositionEnd < next)
				next= linkedPositionEnd;
		} else {
			LinkedPosition nextLinkedPosition= model.findPosition(new LinkedPosition(document, next, 0));
			if (nextLinkedPosition != null) {
				int nextLinkedPositionOffset= nextLinkedPosition.getOffset();
				if (position != nextLinkedPositionOffset && nextLinkedPositionOffset < next)
					next= nextLinkedPositionOffset;
			}
		}
	}

	return next;
}
 
Example #17
Source File: LinkedProposalPositionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TextEdit computeEdits(int offset, LinkedPosition position, char trigger, int stateMask, LinkedModeModel model) throws CoreException {
	ImportRewrite impRewrite= StubUtility.createImportRewrite(fCompilationUnit, true);
	String replaceString= impRewrite.addImport(fTypeProposal);

	MultiTextEdit composedEdit= new MultiTextEdit();
	composedEdit.addChild(new ReplaceEdit(position.getOffset(), position.getLength(), replaceString));
	composedEdit.addChild(impRewrite.rewriteImports(null));
	return composedEdit;
}
 
Example #18
Source File: RenameLinkedMode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public LinkedPosition getCurrentLinkedPosition() {
	Point selection= fEditor.getViewer().getSelectedRange();
	int start= selection.x;
	int end= start + selection.y;
	LinkedPosition[] positions= fLinkedPositionGroup.getPositions();
	for (int i= 0; i < positions.length; i++) {
		LinkedPosition position= positions[i];
		if (position.includes(start) && position.includes(end))
			return position;
	}
	return null;
}
 
Example #19
Source File: RenameLinkedMode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void restoreFullSelection() {
	if (fOriginalSelection.y != 0) {
		int originalOffset= fOriginalSelection.x;
		LinkedPosition[] positions= fLinkedPositionGroup.getPositions();
		for (int i= 0; i < positions.length; i++) {
			LinkedPosition position= positions[i];
			if (! position.isDeleted() && position.includes(originalOffset)) {
				fEditor.getViewer().setSelectedRange(position.offset, position.length);
				return;
			}
		}
	}
}
 
Example #20
Source File: AttributeOrEventProposal.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Special code added to allow tabstop positions so we can easily tab past the quotes for Events/Attributes.
 */
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset)
{
	super.apply(viewer, trigger, stateMask, offset);

	// See if there are any positions that should be linked. Last is always exit, first is cursor position
	if (_positions != null && _positions.length > 0)
	{
		IDocument document = viewer.getDocument();
		boolean validPrefix = isValidPrefix(getPrefix(document, offset), getDisplayString());
		int shift = (validPrefix) ? offset - this._replacementOffset : 0;

		try
		{
			LinkedModeModel.closeAllModels(document); // Exit out of any existing linked mode

			LinkedModeModel model = new LinkedModeModel();
			int i = 0;
			for (int pos : _positions)
			{
				LinkedPositionGroup group = new LinkedPositionGroup();
				group.addPosition(new LinkedPosition(document, (offset - shift) + pos, 0, i++));
				model.addGroup(group);
			}

			model.forceInstall();
			LinkedModeUI ui = new LinkedModeUI(model, viewer);
			ui.setCyclingMode(LinkedModeUI.CYCLE_ALWAYS);
			ui.setExitPosition(viewer, (offset - shift) + _positions[_positions.length - 1], 0, Integer.MAX_VALUE);
			ui.enter();
		}
		catch (BadLocationException e)
		{
			IdeLog.logError(HTMLPlugin.getDefault(), e);
		}
	}
}
 
Example #21
Source File: XMLAttributeProposal.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Special code added to allow tabstop positions so we can easily tab past the quotes for Events/Attributes.
 */
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset)
{
	super.apply(viewer, trigger, stateMask, offset);

	// See if there are any positions that should be linked. Last is always exit, first is cursor position
	if (_positions != null && _positions.length > 0)
	{
		IDocument document = viewer.getDocument();
		boolean validPrefix = isValidPrefix(getPrefix(document, offset), getDisplayString());
		int shift = (validPrefix) ? offset - this._replacementOffset : 0;

		try
		{
			LinkedModeModel.closeAllModels(document); // Exit out of any existing linked mode

			LinkedModeModel model = new LinkedModeModel();
			int i = 0;
			for (int pos : _positions)
			{
				LinkedPositionGroup group = new LinkedPositionGroup();
				group.addPosition(new LinkedPosition(document, (offset - shift) + pos, 0, i++));
				model.addGroup(group);
			}

			model.forceInstall();
			LinkedModeUI ui = new LinkedModeUI(model, viewer);
			ui.setCyclingMode(LinkedModeUI.CYCLE_ALWAYS);
			ui.setExitPosition(viewer, (offset - shift) + _positions[_positions.length - 1], 0, Integer.MAX_VALUE);
			ui.enter();
		}
		catch (BadLocationException e)
		{
			IdeLog.logError(XMLPlugin.getDefault(), e);
		}
	}
}
 
Example #22
Source File: RenameLinkedMode.java    From typescript.java with MIT License 5 votes vote down vote up
public LinkedPosition getCurrentLinkedPosition() {
	Point selection = fEditor.getViewer().getSelectedRange();
	int start = selection.x;
	int end = start + selection.y;
	LinkedPosition[] positions = fLinkedPositionGroup.getPositions();
	for (int i = 0; i < positions.length; i++) {
		LinkedPosition position = positions[i];
		if (position.includes(start) && position.includes(end))
			return position;
	}
	return null;
}
 
Example #23
Source File: RenameLinkedMode.java    From typescript.java with MIT License 5 votes vote down vote up
private void restoreFullSelection() {
	if (fOriginalSelection.y != 0) {
		int originalOffset = fOriginalSelection.x;
		LinkedPosition[] positions = fLinkedPositionGroup.getPositions();
		for (int i = 0; i < positions.length; i++) {
			LinkedPosition position = positions[i];
			if (!position.isDeleted() && position.includes(originalOffset)) {
				fEditor.getViewer().setSelectedRange(position.offset, position.length);
				return;
			}
		}
	}
}
 
Example #24
Source File: TexCompletionProposal.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
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) {
    }
}
 
Example #25
Source File: DefaultLinkedPositionGroupCalculator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected LinkedPositionGroup createLinkedGroupFromReplaceEdits(List<ReplaceEdit> edits, XtextEditor xtextEditor,
		final String originalName, SubMonitor progress) {
	if (edits == null)
		return null;
	final IXtextDocument document = xtextEditor.getDocument();
	LinkedPositionGroup group = new LinkedPositionGroup();
	Iterable<LinkedPosition> linkedPositions = filter(
			Iterables.transform(edits, new Function<ReplaceEdit, LinkedPosition>() {
				@Override
				public LinkedPosition apply(ReplaceEdit edit) {
					try {
						String textToReplace = document.get(edit.getOffset(), edit.getLength());
						int indexOf = textToReplace.indexOf(originalName);
						if (indexOf != -1) {
							int calculatedOffset = edit.getOffset() + indexOf;
							return new LinkedPosition(document, calculatedOffset, originalName.length());
						}
					} catch (BadLocationException exc) {
						LOG.error("Skipping invalid text edit " + notNull(edit), exc);
					}
					return null;
				}
			}), Predicates.notNull());
	progress.worked(10);
	final int invocationOffset = xtextEditor.getInternalSourceViewer().getSelectedRange().x;
	int i = 0;
	for (LinkedPosition position : sortPositions(linkedPositions, invocationOffset)) {
		try {
			position.setSequenceNumber(i);
			i++;
			group.addPosition(position);
		} catch (BadLocationException e) {
			LOG.error(e.getMessage(), e);
			return null;
		}
	}
	return group;
}
 
Example #26
Source File: RenameLinkedMode.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public LinkedPosition getCurrentLinkedPosition() {
	Point selection = editor.getInternalSourceViewer().getSelectedRange();
	int start = selection.x;
	int end = start + selection.y;
	LinkedPosition[] positions = linkedPositionGroup.getPositions();
	for (int i = 0; i < positions.length; i++) {
		LinkedPosition position = positions[i];
		if (position.includes(start) && position.includes(end))
			return position;
	}
	return null;
}
 
Example #27
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Finds the previous position before the given position.
 * 
 * @param position
 *            the current position
 * @return the previous position
 */
protected int findPreviousPosition(int position) {
	ISourceViewer viewer = getSourceViewer();
	int widget = -1;
	int previous = position;
	while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize
		previous = fIterator.preceding(previous);
		if (previous != BreakIterator.DONE)
			widget = modelOffset2WidgetOffset(viewer, previous);
	}

	IDocument document = viewer.getDocument();
	LinkedModeModel model = LinkedModeModel.getModel(document, position);
	if (model != null) {
		LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0));
		if (linkedPosition != null) {
			int linkedPositionOffset = linkedPosition.getOffset();
			if (position != linkedPositionOffset && previous < linkedPositionOffset)
				previous = linkedPositionOffset;
		} else {
			LinkedPosition previousLinkedPosition = model
					.findPosition(new LinkedPosition(document, previous, 0));
			if (previousLinkedPosition != null) {
				int previousLinkedPositionEnd = previousLinkedPosition.getOffset()
						+ previousLinkedPosition.getLength();
				if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd)
					previous = previousLinkedPositionEnd;
			}
		}
	}

	return previous;
}
 
Example #28
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Finds the next position after the given position.
 * 
 * @param position
 *            the current position
 * @return the next position
 */
protected int findNextPosition(int position) {
	ISourceViewer viewer = getSourceViewer();
	int widget = -1;
	int next = position;
	while (next != BreakIterator.DONE && widget == -1) {
		next = fIterator.following(next);
		if (next != BreakIterator.DONE)
			widget = modelOffset2WidgetOffset(viewer, next);
	}

	IDocument document = viewer.getDocument();
	LinkedModeModel model = LinkedModeModel.getModel(document, position);
	if (model != null) {
		LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0));
		if (linkedPosition != null) {
			int linkedPositionEnd = linkedPosition.getOffset() + linkedPosition.getLength();
			if (position != linkedPositionEnd && linkedPositionEnd < next)
				next = linkedPositionEnd;
		} else {
			LinkedPosition nextLinkedPosition = model.findPosition(new LinkedPosition(document, next, 0));
			if (nextLinkedPosition != null) {
				int nextLinkedPositionOffset = nextLinkedPosition.getOffset();
				if (position != nextLinkedPositionOffset && nextLinkedPositionOffset < next)
					next = nextLinkedPositionOffset;
			}
		}
	}

	return next;
}
 
Example #29
Source File: DefaultLinkedPositionGroupCalculator2.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected LinkedPositionGroup createLinkedGroupFromReplaceEdits(List<ReplaceEdit> edits, XtextEditor xtextEditor, String originalName,
		SubMonitor progress) {
	if (edits == null) {
		return null;
	}

	IXtextDocument document = xtextEditor.getDocument();
	LinkedPositionGroup group = new LinkedPositionGroup();
	List<LinkedPosition> linkedPositions = new ArrayList<>();
	edits.forEach(replaceEdit -> {
		try {
			String textToReplace = document.get(replaceEdit.getOffset(), replaceEdit.getLength());
			int indexOf = textToReplace.indexOf(originalName);
			if (indexOf != -1) {
				int calculatedOffset = replaceEdit.getOffset() + indexOf;
				linkedPositions.add(new LinkedPosition(document, calculatedOffset, originalName.length()));
			}
		} catch (BadLocationException exc) {
			LOG.error("Skipping invalid text edit " + replaceEdit, exc);
		}
	});

	progress.worked(10);

	int invocationOffset = xtextEditor.getInternalSourceViewer().getSelectedRange().x;
	int i = 0;
	for (LinkedPosition position : sortPositions(linkedPositions, invocationOffset)) {
		try {
			position.setSequenceNumber(i);
			i++;
			group.addPosition(position);
		} catch (BadLocationException e) {
			LOG.error(e.getMessage(), e);
			return null;
		}
	}
	return group;
}
 
Example #30
Source File: LinkedModelCalculatorIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCorrectTextEditsDeclarationNotInFile() throws Exception {
	String initialModel1 = "A { B ref A } C { D ref A }";
	createFile(pathToFile1, initialModel1);
	String initialModel2 = "E { ref A } F { ref E ref A}";
	IFile file2 = createFile(pathToFile2, initialModel2);
	waitForBuild();
	XtextEditor editor = openEditor(file2);
	EObject a = editor.getDocument().readOnly(new IUnitOfWork<EObject, XtextResource>() {

		@Override
		public EObject exec(XtextResource state) throws Exception {
			return ((Element) state.getContents().get(0).eContents().get(0)).getReferenced().get(0);
		}

	});

	URI uri = EcoreUtil.getURI(a);
	selectElementInEditor(a, uri, editor, (XtextResource) a.eResource());
	IRenameElementContext renameElementContext = new IRenameElementContext.Impl(uri, a.eClass(), editor, editor
			.getSelectionProvider().getSelection(), uriToFile2);
	LinkedPositionGroup linkedPositionGroup = linkedModelCalculator.getLinkedPositionGroup(renameElementContext,
			new NullProgressMonitor()).get();
	LinkedPosition[] positions = linkedPositionGroup.getPositions();
	assertEquals(2, positions.length);

	int[] offsets = { 8, 26 };
	for (int i = 0; i < positions.length; i++) {
		assertEquals(offsets[i], positions[i].getOffset());
	}
}