Java Code Examples for org.eclipse.jface.text.DocumentEvent#getOffset()

The following examples show how to use org.eclipse.jface.text.DocumentEvent#getOffset() . 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: 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 2
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 3
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 4
Source File: SemanticHighlightingPresenter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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(HighlightedPosition 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 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: NonRuleBasedDamagerRepairer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the damage region.
 *
 * @param partition the partition
 * @param event the event
 * @param documentPartitioningChanged the document partitioning changed
 * @return the damage region
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
 */
@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 7
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 8
Source File: ScriptConsoleDocumentListener.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Whenever the document changes, we stop listening to change the document from
 * within this listener (passing commands to the handler if needed, getting results, etc).
 */
@Override
public void documentChanged(DocumentEvent event) {
    lastChangeMillis = System.currentTimeMillis();
    startDisconnected();
    try {
        int eventOffset = event.getOffset();
        String eventText = event.getText();
        proccessAddition(eventOffset, eventText);
    } finally {
        stopDisconnected();
    }
}
 
Example 9
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 10
Source File: NonRuleBasedDamagerRepairer.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent,
 *      boolean)
 */
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 11
Source File: NonRuleBasedDamagerRepairer.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent,
 *      boolean)
 */
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 12
Source File: NonRuleBasedDamagerRepairer.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent,
 *      boolean)
 */
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 13
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 14
Source File: NonRuleBasedDamagerRepairer.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
 */
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: 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 16
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 17
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 18
Source File: NonRuleBasedDamagerRepairer.java    From http4e with Apache License 2.0 5 votes vote down vote up
/**
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
 */
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;
}