Java Code Examples for org.eclipse.jface.text.IDocument#removePosition()

The following examples show how to use org.eclipse.jface.text.IDocument#removePosition() . 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: IndentForTabHandler.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Insert tab/spaces at selection offset and each subsequent line origin
 * 
 * @see com.mulgasoft.emacsplus.commands.EmacsPlusCmdHandler#transform(ITextEditor, IDocument, ITextSelection, ExecutionEvent)
 */
@Override
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection, ExecutionEvent event)
throws BadLocationException {
	// if we're here, either ^U or no relevant ^I
	ColumnSupport cs = new ColumnSupport(document,editor); 
	String tab = cs.getSpaces(0,cs.getTabWidth());
	int off = currentSelection.getOffset();
	Position coff = new Position(getCursorOffset(editor,currentSelection),0);
	try {
		document.addPosition(coff);
		int begin = document.getLineOfOffset(off);
		int end = document.getLineOfOffset(off+ currentSelection.getLength());
		if (begin != end) {
			while (++begin <= end) {
				document.replace(document.getLineOffset(begin), 0, tab);
			}
		}
		document.replace(off, 0, tab);
	} finally {
		document.removePosition(coff);
	} 
	return coff.offset;
}
 
Example 2
Source File: TemplateEngine.java    From typescript.java with MIT License 5 votes vote down vote up
public void reset() {
	fProposals.clear();
	for (Iterator it = fPositions.entrySet().iterator(); it.hasNext();) {
		Entry entry = (Entry) it.next();
		IDocument doc = (IDocument) entry.getKey();
		Position position = (Position) entry.getValue();
		doc.removePosition(position);
	}
	fPositions.clear();
}
 
Example 3
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Called after the document changed occurred. It must be preceded by a call to preReplace().
 *
 * @param document the document on which to track the reference position.
 * @return offset after the replace
 */
public int postReplace(IDocument document) {
	try {
		document.removePosition(CATEGORY, fPosition);
		document.removePositionUpdater(fPositionUpdater);
		document.removePositionCategory(CATEGORY);

	} catch (BadPositionCategoryException e) {
		// should not happen
		JavaPlugin.log(e);
	}
	return fPosition.getOffset();
}
 
Example 4
Source File: TemplateEngine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Empties the collector.
 */
public void reset() {
	fProposals.clear();
	for (Iterator<Entry<IDocument, Position>> it= fPositions.entrySet().iterator(); it.hasNext();) {
		Entry<IDocument, Position> entry= it.next();
		IDocument doc= entry.getKey();
		Position position= entry.getValue();
		doc.removePosition(position);
	}
	fPositions.clear();
}
 
Example 5
Source File: NewLineIndentHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see com.mulgasoft.emacsplus.commands.EmacsPlusCmdHandler#transform(ITextEditor, IDocument, ITextSelection, ExecutionEvent)
 */
@Override
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection, ExecutionEvent event)
throws BadLocationException {
	int result = NO_OFFSET; 
	Control c = MarkUtils.getTextWidget(editor);
	if (c instanceof StyledText) {
		StyledText st = (StyledText)c;
		String ld = st.getLineDelimiter();
		int insertionOffset = getCursorOffset(editor,currentSelection);
		int widgetInsertionOffset = st.getCaretOffset();
		
		// the offset position will be updated by the internals on insertion/indent
		Position caret= new Position(insertionOffset, 0);
		document.addPosition(caret);
		
		st.setSelectionRange(widgetInsertionOffset, 0);
		// operate directly on the widget
		st.replaceTextRange(widgetInsertionOffset, 0, ld);
		document.removePosition(caret);
		
		if (st.getSelection().x == widgetInsertionOffset) {
			// move cursor by the amount detected
			result = getCursorOffset(editor) + caret.offset - insertionOffset;
		} 
	}
	return result;
}
 
Example 6
Source File: TecoRegister.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
private void removePosition(ITextEditor editor, Position position) {
	removeListener();
	IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
	try {
		document.removePosition(MarkRing.MARK_CATEGORY,position);
	} catch (BadPositionCategoryException e) {
	}			
}
 
Example 7
Source File: TemplateEngine.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Empties the collector.
 */
public void reset() {
	fProposals.clear();
	
	for (Entry<IDocument, Position> entry : fPositions.entrySet()) {
		IDocument doc= entry.getKey();
		Position position= entry.getValue();
		doc.removePosition(position);
	}
	fPositions.clear();
}
 
Example 8
Source File: N4JSReplacementTextApplier.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Really do apply the proposal. Assumed to be run within the prevent flickering mode.
 */
private int doApply(QualifiedName qualifiedName, String alias, IDocument document,
		ConfigurableCompletionProposal proposal) throws BadLocationException {
	String shortSemanticReplacementString = alias != null ? alias : lastSegmentOrDefaultHost(qualifiedName);
	String shortSyntacticReplacementString = valueConverter.toString(shortSemanticReplacementString);
	String longReplacementString = shortSyntacticReplacementString
			+ ConfigurableCompletionProposalExtensions.getReplacementSuffix(proposal);

	ImportRewriter importRewriter = importRewriterFactory.create(document, context);

	ReplaceEdit replaceEdit = new ReplaceEdit(
			proposal.getReplacementOffset(),
			proposal.getReplacementLength(),
			longReplacementString);
	MultiTextEdit compound = new MultiTextEdit();
	AliasLocation aliasLocation = null;
	if (alias != null) {
		aliasLocation = importRewriter.addSingleImport(qualifiedName, alias, compound);
	} else {
		importRewriter.addSingleImport(qualifiedName, compound);
	}
	compound.addChild(replaceEdit);

	Position caret = new Position(proposal.getReplacementOffset(), 0);
	document.addPosition(caret);
	compound.apply(document);
	document.removePosition(caret);

	int cursorPosition = caret.getOffset();
	proposal.setReplacementOffset(cursorPosition - longReplacementString.length());
	proposal.setReplacementLength(shortSyntacticReplacementString.length()); // do not include suffix!
	proposal.setCursorPosition(
			cursorPosition - proposal.getReplacementOffset()); // cursorPosition is relative to replacementOffset!

	if (aliasLocation != null) {
		final int aliasOffset = aliasLocation.getBaseOffset() + aliasLocation.getRelativeOffset();
		final int aliasLength = shortSyntacticReplacementString.length();
		N4JSCompletionProposal castedProposal = (N4JSCompletionProposal) proposal;
		castedProposal.setLinkedModeBuilder((appliedProposal, currentDocument) -> {
			if (viewer.getTextWidget() == null || viewer.getTextWidget().isDisposed()) {
				return; // do not attempt to set up linked mode in a disposed UI
			}
			try {
				LinkedPositionGroup group = new LinkedPositionGroup();
				group.addPosition(new LinkedPosition(
						currentDocument,
						aliasOffset,
						aliasLength,
						LinkedPositionGroup.NO_STOP));
				group.addPosition(new LinkedPosition(
						currentDocument,
						proposal.getReplacementOffset(),
						proposal.getReplacementLength(),
						LinkedPositionGroup.NO_STOP));
				proposal.setSelectionStart(proposal.getReplacementOffset());
				proposal.setSelectionLength(proposal.getReplacementLength());
				LinkedModeModel model = new LinkedModeModel();
				model.addGroup(group);
				model.forceInstall();

				LinkedModeUI ui = new LinkedModeUI(model, viewer);
				ui.setExitPolicy(new IdentifierExitPolicy('\n'));
				ui.setExitPosition(
						viewer,
						proposal.getReplacementOffset()
								+ proposal.getCursorPosition()
								+ ConfigurableCompletionProposalExtensions.getCursorOffset(proposal),
						0,
						Integer.MAX_VALUE);
				ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
				ui.enter();
			} catch (BadLocationException e) {
				logger.error(e.getMessage(), e);
			}
		});
	} else {
		adjustCursorPositionIfRequested(proposal);
	}
	return cursorPosition;
}
 
Example 9
Source File: TLAProofPosition.java    From tlaplus with MIT License 4 votes vote down vote up
/**
 * Should be called when this is removed as a fold.
 * 
 * @param document from which this is being deleted
 */
public void remove(IDocument document)
{
    document.removePosition(positionOfProof);
    document.removePosition(positionOfStatement);
}
 
Example 10
Source File: DeleteWhitespaceHandler.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Determine the correct set of lines and delete the whitespace at the end of each of them.
 * 
 * @see com.mulgasoft.emacsplus.commands.EmacsPlusCmdHandler#transform(ITextEditor, IDocument, ITextSelection, ExecutionEvent)
 */
@Override
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection,
		ExecutionEvent event) throws BadLocationException {
	int result = NO_OFFSET;
	int lastLine = 0;
	int firstLine = 0;
	int maxOffset = Integer.MAX_VALUE;		
	if (editor.showsHighlightRangeOnly()) {
		// if the buffer is narrowed, then operate on precise region
		IRegion narrow = editor.getHighlightRange();
		int lastOffset = narrow.getOffset() + narrow.getLength();
		firstLine = document.getLineOfOffset(narrow.getOffset()); 
		lastLine = document.getLineOfOffset(lastOffset);
		IRegion endInfo = document.getLineInformationOfOffset(lastOffset);
		if (endInfo.getOffset() != lastOffset) {
			// point maxOffset at the last character in the narrowed region
			maxOffset = lastOffset -1;
		} else {
			// back up if we're at the first offset of the last line
			lastLine--;
		}
	} else {
		lastLine = document.getNumberOfLines() -1;
	}
	if (firstLine <= lastLine) {
		Position coff = new Position(getCursorOffset(editor,currentSelection),0);
		try {
			// record the position so we can restore it after any whitespace deletions
			document.addPosition(coff);
			deleteWhitepace(lastLine,firstLine,maxOffset,document);
		} catch (BadLocationException e) {
			// shouldn't happen, but alert user if it does
			asyncShowMessage(editor,BAD_LOCATION_ERROR,true);
		} finally {
			result = coff.getOffset();
			document.removePosition(coff);
		}
	}
	return result;
}
 
Example 11
Source File: MarkRing.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
private void removeMarkPosition(IDocument document, Position mark) throws BadPositionCategoryException {
	document.removePosition(MARK_CATEGORY, mark);
}