org.eclipse.jface.text.DocumentEvent Java Examples

The following examples show how to use org.eclipse.jface.text.DocumentEvent. 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: AbstractPyCompletionProposalExtension2.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
    String[] strs = PySelection.getActivationTokenAndQual(document, offset, false);
    //System.out.println("validating:"+strs[0]+" - "+strs[1]);
    //when we end with a '.', we should start a new completion (and not stay in the old one).
    if (strs[1].length() == 0 && (strs[0].length() == 0 || strs[0].endsWith("."))) {
        //System.out.println(false);
        return false;
    }
    String qualifier = strs[1];
    final boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences
            .getUseSubstringMatchInCodeCompletion();
    String displayString = getDisplayString();
    boolean ret = PyCodeCompletionUtils.acceptName(useSubstringMatchInCodeCompletion, displayString, qualifier);
    return ret;
}
 
Example #2
Source File: ConfigurableCompletionProposal.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
	if (event != null) {
		int oldReplaceContextLength = getReplaceContextLength();
		int diff = event.getText().length() - event.getLength();
		setReplaceContextLength(oldReplaceContextLength + diff);
	}
	try {
		String prefix = document.get(replacementOffset, offset - replacementOffset);
		return matcher.isCandidateMatchingPrefix(replacementString, prefix);
	}
	catch (BadLocationException e) {
		log.info(e.getMessage(), e);
		return false;
	}
}
 
Example #3
Source File: StyleRanges.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
public List<StyleRange> getRanges(String expression) {
	final List<StyleRange> ranges = Lists.newArrayList();
	DocumentEvent event = new DocumentEvent();
	event.fDocument = new DummyDocument(expression);
	DocumentTokenSource tokenSource = tokenSourceProvider.get();
	tokenSource.updateStructure(event);
	Iterator<ILexerTokenRegion> iterator = tokenSource.getTokenInfos().iterator();
	while (iterator.hasNext()) {
		ILexerTokenRegion next = iterator.next();
		TextAttribute attribute = attributeProvider.getAttribute(tokenTypeMapper.getId(next.getLexerTokenType()));
		StyleRange range = new StyleRange(next.getOffset(), next.getLength(), attribute.getForeground(),
				attribute.getBackground());
		range.font = attribute.getFont();
		range.fontStyle = attribute.getStyle();
		ranges.add(range);
	}
	return merge(ranges);
}
 
Example #4
Source File: SemanticHighlightingDiffCalculator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
protected int getLineShift(IDocument oldState, DocumentEvent event) throws BadLocationException {
	// Insert edit.
	if (event.fLength == 0) {
		Preconditions.checkNotNull(event.fText, "fText");
		int beforeEndLine = event.fDocument.getLineOfOffset(event.fOffset + event.fLength);
		int afterEndLine = event.fDocument.getLineOfOffset(event.fOffset + event.fText.length());
		return afterEndLine - beforeEndLine;
	// Delete edit.
	} else if (event.fText == null || event.fText.isEmpty()) {
		return event.fDocument.getLineOfOffset(event.fOffset) - oldState.getLineOfOffset(event.fOffset + event.fLength);
	// Replace edit.
	} else {
		int startLine = oldState.getLineOfOffset(event.fOffset + event.fLength);
		int lineShift = event.fDocument.getLineOfOffset(event.fOffset + event.fText.length()) - startLine;
		return lineShift;
	}
}
 
Example #5
Source File: TypingRunDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Computes the change abstraction given a text event.
 *
 * @param event the text event to analyze
 * @return a change object describing the event
 */
private Change computeChange(TextEvent event) {
	DocumentEvent e= event.getDocumentEvent();
	if (e == null)
		return new Change(TypingRun.NO_CHANGE, -1);

	int start= e.getOffset();
	int end= e.getOffset() + e.getLength();
	String newText= e.getText();
	if (newText == null)
		newText= new String();

	if (start == end) {
		// no replace / delete / overwrite
		if (newText.length() == 1)
			return new Change(TypingRun.INSERT, end + 1);
	} else if (start == end - 1) {
		if (newText.length() == 1)
			return new Change(TypingRun.OVERTYPE, end);
		if (newText.length() == 0)
			return new Change(TypingRun.DELETE, start);
	}

	return new Change(TypingRun.UNKNOWN, -1);
}
 
Example #6
Source File: KillRing.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Determine if a selection is being replaced by non-emacs+ behavior (or YANK), and save the
 * replaced content in the kill ring. This captures the Eclipse (but not emacs) behavior where
 * typing/pasting into a selection replaces the old with the new, so it is appropriate to save
 * the old text to the kill ring.
 * 
 * @param event the DocumentEvent containing the IDocument, offset, and length
 * @return true if the non-zero length region matches the current selection in the editor
 */
private boolean isSelectionReplace(DocumentEvent event) {
	int len = event.getLength();
	// ignore plain insertion or any emacs+ (except YANK) command invocation
	if (selectionReplace &&  len > 0 && shouldSave()) {
		ITextEditor editor = EmacsPlusUtils.getCurrentEditor();
		// otherwise, if we can get the selection, see if it matches the replace region
		if (editor != null && editor.getDocumentProvider().getDocument(editor.getEditorInput()) == event.getDocument()) {
			ISelection isel = editor.getSelectionProvider().getSelection();
			if (isel instanceof ITextSelection) {
				ITextSelection selection = (ITextSelection)isel;
				boolean result = selection.getOffset() == event.getOffset() && selection.getLength() == len;
				return result;
			}
		}
	}
	return false;
}
 
Example #7
Source File: DamageRepairer.java    From LogViewer with Eclipse Public License 2.0 6 votes vote down vote up
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, boolean documentPartitioningChanged) {
    if (!documentPartitioningChanged) {
        try {

            IRegion info= document.getLineInformationOfOffset(event.getOffset());
            int start= Math.max(partition.getOffset(), info.getOffset());

            int end= event.getOffset() + (event.getText() == null ? event.getLength() : event.getText().length());

            if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
                // optimize the case of the same line
                end= info.getOffset() + info.getLength();
            } else
                end= endOfLineOf(end);

            end= Math.min(partition.getOffset() + partition.getLength(), end);
            return new Region(start, end - start);

        } catch (BadLocationException x) {
            logger.logInfo("unable to find location in document to repair a given region",x); //$NON-NLS-1$
        }
    }
    return partition;
}
 
Example #8
Source File: JsonEditor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void documentChanged(DocumentEvent event) {
    if (event.getDocument() instanceof JsonDocument) {
        final JsonDocument document = (JsonDocument) event.getDocument();

        document.onChange();
        Display.getCurrent().asyncExec(new Runnable() {
            @Override
            public void run() {
                if (contentOutline != null) {
                    // depends on the results of document.onChange()
                    contentOutline.setInput(getEditorInput());
                }
            }
        });
        // depends on the results of document.onChange()
        runValidate(false);
    }
}
 
Example #9
Source File: LangCompletionProposal.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
	if(offset < getReplaceOffset())
		return false;
	
	String prefix;
	try {
		prefix = document.get(getReplaceOffset(), offset - getReplaceOffset());
	} catch (BadLocationException e) {
		return false;
	}
	boolean validPrefix = isValidPrefix(prefix);
	
	if(validPrefix && event != null) {
		// adapt replacement length to document event/ 
		int eventEndOffset = event.fOffset + event.fLength;
		// The event should be a common prefix completion (this should be true anyways) :
		int replaceEndPos = getReplaceOffset() + getReplaceLength();
		if(event.fOffset >= getReplaceOffset() && eventEndOffset <= replaceEndPos) {
			int delta = (event.fText == null ? 0 : event.fText.length()) - event.fLength;
			this.replaceLength = Math.max(getReplaceLength() + delta, 0);
		}
	}
	
	return validPrefix;
}
 
Example #10
Source File: HighlightingPresenter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Update the given position with the given event. The event overlaps with the start of the position.
 * 
 * @param position
 *            The position
 * @param event
 *            The event
 */
private void updateWithOverStartEvent(AttributedPosition position, DocumentEvent event) {
	int eventOffset = event.getOffset();
	int eventEnd = eventOffset + event.getLength();

	String newText = event.getText();
	if (newText == null)
		newText = ""; //$NON-NLS-1$
	int eventNewLength = newText.length();

	int excludedLength = eventNewLength;
	while (excludedLength > 0 && Character.isJavaIdentifierPart(newText.charAt(excludedLength - 1)))
		excludedLength--;
	int deleted = eventEnd - position.getOffset();
	int inserted = eventNewLength - excludedLength;
	position.update(eventOffset + excludedLength, position.getLength() - deleted + inserted);
}
 
Example #11
Source File: TLAFastPartitioner.java    From tlaplus with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * May be extended by subclasses.
 * </p>
 */
public void documentAboutToBeChanged(DocumentEvent e) {
    if (fIsInitialized) {

        Assert.isTrue(e.getDocument() == fDocument);

        fPreviousDocumentLength= e.getDocument().getLength();
        fStartOffset= -1;
        fEndOffset= -1;
        fDeleteOffset= -1;
    }
}
 
Example #12
Source File: XtextDocumentProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void documentChanged(DocumentEvent event) {
	if (element.fCanBeSaved && modificationStamp == event.getModificationStamp()) {
		element.fCanBeSaved = false;
		fireElementDirtyStateChanged(element.fElement, element.fCanBeSaved);
	} else if (!element.fCanBeSaved) {
		element.fCanBeSaved = true;
		fireElementDirtyStateChanged(element.fElement, element.fCanBeSaved);
	}
}
 
Example #13
Source File: SemanticHighlightingService.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public HighlightedPositionDiffContext(
		IDocument oldState,
		DocumentEvent event,
		Iterable<? extends HighlightedPositionCore> oldPositions,
		Iterable<? extends HighlightedPositionCore> newPositions) {

	this.oldState = oldState;
	this.newState = event.fDocument;
	this.event = event;
	this.oldPositions = ImmutableList.copyOf(oldPositions);
	this.newPositions = ImmutableList.copyOf(newPositions);
}
 
Example #14
Source File: NonRuleBasedDamagerRepairer.java    From codeexamples-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public IRegion getDamageRegion(
	ITypedRegion partition,
	DocumentEvent event,
	boolean documentPartitioningChanged) {
	if (!documentPartitioningChanged) {
		try {

			IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
			int start = Math.max(partition.getOffset(), info.getOffset());

			int end =
				event.getOffset()
					+ (event.getText() == null
						? event.getLength()
						: event.getText().length());

			if (info.getOffset() <= end
				&& end <= info.getOffset() + info.getLength()) {
				// optimize the case of the same line
				end = info.getOffset() + info.getLength();
			} else
				end = endOfLineOf(end);

			end =
				Math.min(
					partition.getOffset() + partition.getLength(),
					end);
			return new Region(start, end - start);

		} catch (BadLocationException x) {
		}
	}

	return partition;
}
 
Example #15
Source File: ConsoleTail.java    From LogViewer with Eclipse Public License 2.0 5 votes vote down vote up
public synchronized void run() {
	isRunning = true;
	try {
		int readwait = LogViewerPlugin.getDefault().getPreferenceStore().getInt(ILogViewerConstants.PREF_READWAIT);
		doc = openConsole();
		if(doc != null) {
			doc.addDocumentListener(this);
			if (isFirstTimeRead) {
				listener.fileChanged(LogViewerPlugin.getResourceString("tail.loading.file",new String[]{path}).toCharArray(),true);
				DocumentEvent event = new DocumentEvent();
				event.fText = doc.get();
				documentAboutToBeChanged(event);
				documentChanged(event);
			}
		} else {
			throw new ThreadInterruptedException("document was null"); //$NON-NLS-1$
		}
		while(isRunning) {
			wait(readwait);
		}
	} catch(ThreadInterruptedException tie) {
		logger.logError(tie);
		listener.fileChanged(LogViewerPlugin.getResourceString("tail.loading.file.error",new String[]{path}).toCharArray(),true);
	} catch(InterruptedException ie) {
		logger.logError(ie);
	} catch(NullPointerException npe) {
		logger.logError(npe);
		npe.printStackTrace();
	} finally {
		try {
			if(doc != null) {
				doc.removeDocumentListener(this);
				isFirstTimeRead = true;
			}
		} catch(Exception e) {
			// ignore this
		}
	}
	isRunning = false;
}
 
Example #16
Source File: TMPresentationReconcilerTestGenerator.java    From tm4e with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void documentChanged(DocumentEvent event) {

	String command = "document.replace(" + event.getOffset() + ", " + event.getLength() + ", \""
			+ toText(event.getText()) + "\");";
	write("\t\t" + command, true);

	//commands.add(new Command(command));
}
 
Example #17
Source File: ContractConstraintExpressionWizardPageTest.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_documentChanged_set_expression_value() throws Exception {
    wizardPage.createControl(composite);
    final Document document = new Document("name.length() > 25");
    final DocumentEvent event = new DocumentEvent(document, 0, document.getLength(), document.get());
    wizardPage.documentChanged(event);
    assertThat(constraint.getExpression()).isEqualTo(document.get());
}
 
Example #18
Source File: PositionBasedCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean validate(IDocument document, int offset, DocumentEvent event) {
	try {
		String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
		if (fReplacementString.startsWith(content))
			return true;
	} catch (BadLocationException e) {
		// ignore concurrently modified document
	}
	return false;
}
 
Example #19
Source File: PyDefaultDamagerRepairer.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * This implementation damages entire lines unless clipped by the given partition.
 * </p>
 *
 * @return the full lines containing the document changes described by the document event,
 *         clipped by the given partition. If there was a partitioning change then the whole
 *         partition is returned.
 */
@Override
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) {

    if (!documentPartitioningChanged) {
        try {

            IRegion info = fDocument.getLineInformationOfOffset(e.getOffset());
            int start = Math.max(partition.getOffset(), info.getOffset());

            int end = e.getOffset() + (e.getText() == null ? e.getLength() : e.getText().length());

            if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
                // optimize the case of the same line
                end = info.getOffset() + info.getLength();
            } else {
                end = endOfLineOf(end);
            }

            end = Math.min(partition.getOffset() + partition.getLength(), end);
            return new Region(start, end - start);

        } catch (BadLocationException x) {
        }
    }

    return partition;
}
 
Example #20
Source File: OrganizeImportsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IEditingSupport createViewerHelper() {
	return new IEditingSupport() {
		public boolean isOriginator(DocumentEvent event, IRegion subjectRegion) {
			return true; // assume true, since we only register while we are active
		}
		public boolean ownsFocusShell() {
			return fIsQueryShowing;
		}

	};
}
 
Example #21
Source File: SemanticHighlightingPresenter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Update the given position with the given event. The event precedes the position.
 *
 * @param position The position
 * @param event The event
 */
private void updateWithPrecedingEvent(HighlightedPosition position, DocumentEvent event) {
	String newText= event.getText();
	int eventNewLength= newText != null ? newText.length() : 0;
	int deltaLength= eventNewLength - event.getLength();

	position.setOffset(position.getOffset() + deltaLength);
}
 
Example #22
Source File: PresentationDamager.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return <code>true</code> only if the lastDamage is encloses the affected text of the given DocumentEvent.
 */
protected boolean isEventMatchingLastDamage(DocumentEvent e, IRegion lastDamage) {
	int eventStart = e.getOffset();
	int eventEnd = eventStart+e.getText().length();
	int damageStart = lastDamage.getOffset();
	int damageEnd = damageStart+lastDamage.getLength();
	boolean result = damageStart<=eventStart && damageEnd>=eventEnd;
	return result;
}
 
Example #23
Source File: XtextReconciler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void handleDocumentChanged(DocumentEvent event) {
	/*
	 * The reconciler does not partake in the general 
	 * model listener cancelation, so it has to cancel 
	 * itself on document changes
	 */
	cancel();
	if (log.isTraceEnabled())
		log.trace("Reconciler cancelled");
	reallyEnqueueEvent(event);
	schedule(delay);
	if (log.isTraceEnabled())
		log.trace("Reconciler scheduled with delay: " + delay);
}
 
Example #24
Source File: PositionBasedCompletionProposal.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public boolean validate(IDocument document, int offset, DocumentEvent event)
{
	try
	{
		String content = document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
		if (fReplacementString.startsWith(content))
			return true;
	}
	catch (BadLocationException e)
	{
		// ignore concurrently modified document
	}
	return false;
}
 
Example #25
Source File: DocumentTokenSource.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected RepairEntryData getRepairEntryData(DocumentEvent e) throws Exception {
	int tokenStartsAt = 0;
	int tokenInfoIdx = 0;
	for(tokenInfoIdx = 0; tokenInfoIdx< getInternalModifyableTokenInfos().size(); ++tokenInfoIdx) {
		TokenInfo oldToken = getInternalModifyableTokenInfos().get(tokenInfoIdx);
		if(tokenStartsAt <= e.getOffset() && tokenStartsAt + oldToken.getLength() >= e.getOffset())
			break;
		tokenStartsAt += oldToken.getLength();
	}
	final TokenSource delegate = createTokenSource(e.fDocument.get(tokenStartsAt, e.fDocument.getLength() - tokenStartsAt));
	final int offset = tokenStartsAt;
	TokenSource source = new TokenSource() {
		@Override
		public Token nextToken() {
			CommonToken commonToken = (CommonToken) delegate.nextToken();
			commonToken.setText(commonToken.getText());
			commonToken.setStartIndex(commonToken.getStartIndex()+offset);
			commonToken.setStopIndex(commonToken.getStopIndex()+offset);
			return commonToken;
		}

		@Override
		public String getSourceName() {
			return delegate.getSourceName();
		}
	};
	final CommonToken token = (CommonToken) source.nextToken();
	return new RepairEntryData(offset, tokenInfoIdx, token, source);
}
 
Example #26
Source File: XtextReconciler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.7
 */
public void forceReconcile() {
	if(editor != null && editor.getDocument() != null) {
		DocumentEvent dummyEvent = new DocumentEvent(editor.getDocument(), 0, 0, "");
		handleDocumentChanged(dummyEvent);
	}
}
 
Example #27
Source File: HighlightingPresenter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Update the given position with the given event. The event overlaps with the end of the position.
 * 
 * @param position
 *            The position
 * @param event
 *            The event
 */
private void updateWithOverEndEvent(AttributedPosition position, DocumentEvent event) {
	String newText = event.getText();
	if (newText == null)
		newText = ""; //$NON-NLS-1$
	int eventNewLength = newText.length();

	int includedLength = 0;
	while (includedLength < eventNewLength && Character.isJavaIdentifierPart(newText.charAt(includedLength)))
		includedLength++;
	position.setLength(event.getOffset() - position.getOffset() + includedLength);
}
 
Example #28
Source File: XtextDocument.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void fireDocumentChanged(DocumentEvent event) {
	new DisplayRunnable() {
		@Override
		protected void run() throws Exception {
			cancelReaders(resource);
			tokenSource.updateStructure(event);
			XtextDocument.super.fireDocumentChanged(event);
		}
	}.syncExec();
}
 
Example #29
Source File: IDETypeScriptFile.java    From typescript.java with MIT License 5 votes vote down vote up
@Override
public void documentAboutToBeChanged(DocumentEvent event) {
	if (isDisableChanged()) {
		return;
	}
	setDirty(true);
	if (getProject().getProjectSettings().getSynchStrategy() == SynchStrategy.CHANGE) {
		synchronized (synchLock) {
			try {
				String newText = event.getText();
				int position = event.getOffset();

				Location loc = getLocation(position);
				int line = loc.getLine();
				int offset = loc.getOffset();

				Location endLoc = getLocation(position + event.getLength());
				int endLine = endLoc.getLine();
				int endOffset = endLoc.getOffset();

				getProject().getClient().changeFile(getName(), line, offset, endLine, endOffset, newText);
			} catch (Throwable e) {
				e.printStackTrace();
			} finally {
				setDirty(false);
				synchLock.notifyAll();
			}
		}
	}
}
 
Example #30
Source File: SimpleAssistProposal.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
    String[] strs = PySelection.getActivationTokenAndQual(document, offset, false);

    String activationToken = strs[0];
    String qualifier = strs[1];

    if (activationToken.equals("") && qualifier.equals("") == false) {
        if (fReplacementString.startsWith(qualifier) && !fReplacementString.equals(qualifier)) {
            return true;
        }
    }

    return false;
}