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

The following examples show how to use org.eclipse.jface.text.link.LinkedModeModel. 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: 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 #2
Source File: BracketInserter.java    From typescript.java with MIT License 6 votes vote down vote up
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {

			if (fSize == fStack.size() && !isMasked(offset)) {
				if (event.character == fExitCharacter) {
					BracketLevel level = (BracketLevel) fStack.peek();
					if (level.fFirstPosition.offset > offset || level.fSecondPosition.offset < offset)
						return null;
					if (level.fSecondPosition.offset == offset && length == 0)
						// don't enter the character if if its the closing peer
						return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
				}
				// when entering an anonymous class between the parenthesis', we
				// don't want
				// to jump after the closing parenthesis when return is pressed
				if (event.character == SWT.CR && offset > 0) {
					IDocument document = getSourceViewer().getDocument();
					try {
						if (document.getChar(offset - 1) == '{')
							return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
					} catch (BadLocationException e) {
					}
				}
			}
			return null;
		}
 
Example #3
Source File: ExitPolicy.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length)
{
	if (shouldInsertNewline())
	{
		if (event.character == '\n' || event.character == '\r')
		{
			return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
		}
	}

	if (event.character != fExitCharacter)
		return null;

	if (fSize == fStack.size() && !isEscaped(offset))
	{
		BracketLevel level = fStack.peek();
		if (offset < level.fFirstPosition.offset || level.fSecondPosition.offset < offset)
			return null;
		if (level.fSecondPosition.offset == offset && length == 0)
			// don't enter the character if it is the closing peer
			return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
	}

	return null;
}
 
Example #4
Source File: CompilationUnitEditor.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 (fSize == fStack.size() && !isMasked(offset)) {
				if (event.character == fExitCharacter) {
					BracketLevel level= fStack.peek();
					if (level.fFirstPosition.offset > offset || level.fSecondPosition.offset < offset)
						return null;
					if (level.fSecondPosition.offset == offset && length == 0)
						// don't enter the character if if its the closing peer
						return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
				}
				// when entering an anonymous class between the parenthesis', we don't want
				// to jump after the closing parenthesis when return is pressed
				if (event.character == SWT.CR && offset > 0) {
					IDocument document= getSourceViewer().getDocument();
					try {
						if (document.getChar(offset - 1) == '{')
							return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
					} catch (BadLocationException e) {
					}
				}
			}
			return null;
		}
 
Example #5
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 #6
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 #7
Source File: BracketInserter.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
    
    if (fSize == fStack.size() && !isMasked(offset)) {
        if (event.character == fExitCharacter) {
            BracketLevel level= (BracketLevel) fStack.peek();
            if (level.fFirstPosition.offset > offset || level.fSecondPosition.offset < offset)
                return null;
            if (level.fSecondPosition.offset == offset && length == 0)
                // don't enter the character if if its the closing peer
                return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
        }
        // when entering an anonymous class between the parenthesis', we don't want
        // to jump after the closing parenthesis when return is pressed
        if (event.character == SWT.CR && offset > 0) {
            IDocument document= sourceViewer.getDocument();
            try {
                if (document.getChar(offset - 1) == '{')
                    return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
            } catch (BadLocationException e) {
            }
        }
    }
    return null;
}
 
Example #8
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 #9
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 #10
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 #11
Source File: ConfigurableCompletionProposal.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ExitFlags doExit(LinkedModeModel environment, VerifyEvent event, int offset, int length) {
	if (event.character == '\0')
		return null;
	for (char c: exitCharacters) {
		if (event.character == c) {
			return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
		}
	}

	switch (event.character) {
		case SWT.CR:
			return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
		default:
			return null;
	}
}
 
Example #12
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 #13
Source File: LangCompletionProposal.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
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();
}
 
Example #14
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 #15
Source File: IdentifierExitPolicy.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * If the entered character is not a valid identifier part, it is processed after linked editing has been quit.
 * Exceptions are the {@link #exitCharacters} that have been passed into the constructor.
 */
@Override
public ExitFlags doExit(LinkedModeModel environment, VerifyEvent event, int offset, int length) {
	if (event.character == '\0')
		return null;
	for (char c : exitCharacters) {
		if (event.character == c) {
			return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
		}
	}
	switch (event.character) {
	case SWT.CR:
		return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
	default: {
		if (!Character.isJavaIdentifierPart(event.character)) {
			return new ExitFlags(ILinkedModeListener.UPDATE_CARET, true);
		}
		return null;
	}
	}
}
 
Example #16
Source File: LangCompletionProposal.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
	switch (event.character) {
	case SWT.CR:
		int endOfReplacement = getReplaceOffset() + getEffectiveReplaceString(false).length();
		if(offset == endOfReplacement) {
			return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
		}
		return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
	}
	return null;
}
 
Example #17
Source File: RenameLinkedMode.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public boolean start(IRenameElementContext renameElementContext, Provider<LinkedPositionGroup> provider, IProgressMonitor monitor) {
	if (renameElementContext == null)
		throw new IllegalArgumentException("RenameElementContext is null");
	this.linkedPositionGroup = provider.get();
	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);
	}
}
 
Example #18
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 #19
Source File: TypeScriptCompletionProposal.java    From typescript.java with MIT License 5 votes vote down vote up
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 an anonymous class 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;
			}
		}
 
Example #20
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 #21
Source File: RenameLinkedMode.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void left(LinkedModeModel model, int flags) {
	//boolean isValidNewName = updateNewName();
	if ((flags & ILinkedModeListener.UPDATE_CARET) != 0) {// && isValidNewName) {
		if (showPreview)
			controller.startRefactoring(RefactoringType.REFACTORING_PREVIEW);
		else
			controller.startRefactoring(RefactoringType.REFACTORING_DIRECT);
	} else {
		controller.cancelLinkedMode();
	}
}
 
Example #22
Source File: RenameLinkedMode.java    From typescript.java with MIT License 5 votes vote down vote up
@Override
public void left(LinkedModeModel model, int flags) {
	linkedModeLeft();
	if ((flags & ILinkedModeListener.UPDATE_CARET) != 0) {
		doRename(fShowPreview);
	}
}
 
Example #23
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 #24
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 #25
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 #26
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 #27
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 #28
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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:
					// 1) when entering an anonymous class as a parameter, we don't want
					// to jump after the parenthesis when return is pressed
					// 2) after auto completion of methods without parameters, exit from linked mode when return is pressed
					if (offset > 0) {
						try {
							char prevOffsetChar= fDocument.getChar(offset - 1);
							if (prevOffsetChar == '{' || prevOffsetChar == ';')
								return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
						} catch (BadLocationException e) {
						}
					}
					return null;
				default:
					return null;
			}
		}
 
Example #29
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 #30
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;
}