Java Code Examples for org.eclipse.jface.text.TextUtilities#getContentType()

The following examples show how to use org.eclipse.jface.text.TextUtilities#getContentType() . 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: ImportsAwareClipboardAction.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Should not add imports when pasting into a {@link XStringLiteral} or Comments (except of JavaDoc)
 * 
 * @param document
 *            - {@link IDocument} to work with
 * @param caretOffset
 *            - current caret offset
 */
protected boolean shouldAddImports(IDocument document, int caretOffset) {
	if (caretOffset == 0) {
		return true;
	}
	String typeRight = IDocument.DEFAULT_CONTENT_TYPE;
	String typeLeft = IDocument.DEFAULT_CONTENT_TYPE;
	try {
		typeRight = TextUtilities.getContentType(document, IDocumentExtension3.DEFAULT_PARTITIONING, caretOffset,
				false);
		typeLeft = TextUtilities.getContentType(document, IDocumentExtension3.DEFAULT_PARTITIONING,
				caretOffset > 0 ? caretOffset - 1 : caretOffset, false);
	} catch (BadLocationException exception) {
		// Should not happen
	}
	if (COMMENT_PARTITION.equals(typeRight) || STRING_LITERAL_PARTITION.equals(typeRight)
			|| SL_COMMENT_PARTITION.equals(typeRight) || "__rich_string".equals(typeRight)) {
		if (typeLeft.equals(typeRight))
			return false;
	}
	return true;
}
 
Example 2
Source File: HackDefaultCharacterPairMatcher.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
protected IRegion performMatch(IDocument doc, int caretOffset) throws BadLocationException {
	final int charOffset= caretOffset - 1;
	final char prevChar= doc.getChar(Math.max(charOffset, 0));
	if (!fPairs.contains(prevChar)) return null;
	final boolean isForward= fPairs.isStartCharacter(prevChar);
	fAnchor= isForward ? ICharacterPairMatcher.LEFT : ICharacterPairMatcher.RIGHT;
	final int searchStartPosition= isForward ? caretOffset : caretOffset - 2;
	final int adjustedOffset= isForward ? charOffset : caretOffset;
	final String partition= TextUtilities.getContentType(doc, fPartitioning, charOffset, false);
	final DocumentPartitionAccessor partDoc= new DocumentPartitionAccessor(doc, fPartitioning, partition);
	int endOffset= findMatchingPeer(partDoc, prevChar, fPairs.getMatching(prevChar),
			isForward,  isForward ? doc.getLength() : -1,
			searchStartPosition);
	if (endOffset == -1) return null;
	final int adjustedEndOffset= isForward ? endOffset + 1: endOffset;
	if (adjustedEndOffset == adjustedOffset) return null;
	return new Region(Math.min(adjustedOffset, adjustedEndOffset),
			Math.abs(adjustedEndOffset - adjustedOffset));
}
 
Example 3
Source File: TemplateCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	try {
		String partition= TextUtilities.getContentType(context.getDocument(), IJavaPartitions.JAVA_PARTITIONING, context.getInvocationOffset(), true);
		if (partition.equals(IJavaPartitions.JAVA_DOC))
			return fJavadocTemplateEngine;
		else {
			CompletionContext coreContext= context.getCoreContext();
			if (coreContext != null) {
				int tokenLocation= coreContext.getTokenLocation();
				if ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0) {
					return fJavaMembersTemplateEngine;
				}
				if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
					return fJavaStatementsTemplateEngine;
				}
			}
			return fJavaTemplateEngine;
		}
	} catch (BadLocationException x) {
		return null;
	}
}
 
Example 4
Source File: SpecificContentAssistAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
* Computes the partition type at the selection start and checks whether the proposal category
* has any computers for this partition.
*
* @param selection the selection
* @return <code>true</code> if there are any computers for the selection
*/
  private boolean isValidSelection(ISelection selection) {
  	if (!(selection instanceof ITextSelection))
  		return false;
  	int offset= ((ITextSelection) selection).getOffset();

  	IDocument document= getDocument();
  	if (document == null)
  		return false;

  	String contentType;
  	try {
       contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
      } catch (BadLocationException x) {
      	return false;
      }

      return fCategory.hasComputers(contentType);
  }
 
Example 5
Source File: ContentAssistant.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the code assist processor for the content type of the specified document position.
 * 
 * @param contentAssistSubjectControl
 *            the code assist subject control
 * @param offset
 *            a offset within the document
 * @return a content-assist processor or <code>null</code> if none exists
 * @since 3.0
 */
private IContentAssistProcessor getProcessor(IContentAssistSubjectControl contentAssistSubjectControl, int offset)
{
	try
	{

		IDocument document = contentAssistSubjectControl.getDocument();
		String type;
		if (document != null)
		{
			type = TextUtilities.getContentType(document, getDocumentPartitioning(), offset, true);
		}
		else
		{
			type = IDocument.DEFAULT_CONTENT_TYPE;
		}

		return getContentAssistProcessor(type);

	}
	catch (BadLocationException x)
	{
	}

	return null;
}
 
Example 6
Source File: ContentAssistant.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the code assist processor for the content type of the specified document position.
 * 
 * @param viewer
 *            the text viewer
 * @param offset
 *            a offset within the document
 * @return a content-assist processor or <code>null</code> if none exists
 * @since 3.0
 */
public IContentAssistProcessor getProcessor(ITextViewer viewer, int offset)
{
	try
	{

		IDocument document = viewer.getDocument();
		String type = TextUtilities.getContentType(document, getDocumentPartitioning(), offset, true);

		return getContentAssistProcessor(type);

	}
	catch (BadLocationException x)
	{
	}

	return null;
}
 
Example 7
Source File: ReactTemplateCompletionProposalComputer.java    From typescript.java with MIT License 6 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(TypeScriptContentAssistInvocationContext context) {
	try {
		if (!TypeScriptResourceUtil.isTsxOrJsxFile(context.getResource())) {
			return null;
		}
		String partition = TextUtilities.getContentType(context.getDocument(),
				IJavaScriptPartitions.JAVA_PARTITIONING, context.getInvocationOffset(), true);
		if (partition.equals(IJavaScriptPartitions.JAVA_DOC)) {
			return null;
		} else if (partition.equals(IJSXPartitions.JSX)) {
			return null;
		} else {
			return reactTemplateEngine;
		}
	} catch (BadLocationException x) {
		return null;
	}
}
 
Example 8
Source File: TypeScriptAutoIndentStrategy.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns the indentation of the line <code>line</code> in <code>document</code>.
 * The returned string may contain pairs of leading slashes that are considered
 * part of the indentation. The space before the asterisk in a javadoc-like
 * comment is not considered part of the indentation.
 *
 * @param document the document
 * @param line the line
 * @return the indentation of <code>line</code> in <code>document</code>
 * @throws BadLocationException if the document is changed concurrently
 */
private static String getCurrentIndent(Document document, int line) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int from= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// go behind line comments
	int to= from;
	while (to < endOffset - 2 && document.get(to, 2).equals(LINE_COMMENT))
		to += 2;

	while (to < endOffset) {
		char ch= document.getChar(to);
		if (!Character.isWhitespace(ch))
			break;
		to++;
	}

	// don't count the space before javadoc like, asterisk-style comment lines
	if (to > from && to < endOffset - 1 && document.get(to - 1, 2).equals(" *")) { //$NON-NLS-1$
		String type= TextUtilities.getContentType(document, IJavaScriptPartitions.JAVA_PARTITIONING, to, true);
		if (type.equals(IJavaScriptPartitions.JAVA_DOC) || type.equals(IJavaScriptPartitions.JAVA_MULTI_LINE_COMMENT))
			to--;
	}

	return document.get(from, to - from);
}
 
Example 9
Source File: JavaTemplatesPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the active contexts for the given position in the document.
 * <p>
 * FIXME: should trigger code assist to get the context.
 * </p>
 * 
 * @param document the document
 * @param offset the offset
 * @return an array of valid context id
 */
@Override
protected String[] getContextTypeIds(IDocument document, int offset) {
	try {
		String partition= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
		String[] ids= new String[] { JavaContextType.ID_ALL, JavaContextType.ID_MEMBERS, JavaContextType.ID_STATEMENTS, SWTContextType.ID_ALL, SWTContextType.ID_STATEMENTS, SWTContextType.ID_MEMBERS};
		if (partition.equals(IJavaPartitions.JAVA_DOC))
			ids= new String[] { JavaDocContextType.ID };
		return ids;
	} catch (BadLocationException e) {
		return new String[0];
	}
}
 
Example 10
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected int getLineStartPosition(final IDocument document, final String line, final int length, final int offset) {

	String type= IDocument.DEFAULT_CONTENT_TYPE;
	try {
		type= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
	} catch (BadLocationException exception) {
		// Should not happen
	}

	int index= super.getLineStartPosition(document, line, length, offset);
	if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT)) {
		if (index < length - 1 && line.charAt(index) == '*' && line.charAt(index + 1) != '/') {
			do {
				++index;
			} while (index < length && Character.isWhitespace(line.charAt(index)));
		}
	} else {
		if (index < length - 1 && line.charAt(index) == '/' && line.charAt(index + 1) == '/') {
			index++;
			do {
				++index;
			} while (index < length && Character.isWhitespace(line.charAt(index)));
		}
	}
	return index;
}
 
Example 11
Source File: IndentUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the indentation of the line <code>line</code> in <code>document</code>.
 * The returned string may contain pairs of leading slashes that are considered
 * part of the indentation. The space before the asterix in a javadoc-like
 * comment is not considered part of the indentation.
 *
 * @param document the document
 * @param line the line
 * @return the indentation of <code>line</code> in <code>document</code>
 * @throws BadLocationException if the document is changed concurrently
 */
private static String getCurrentIndent(IDocument document, int line) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int from= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// go behind line comments
	int to= from;
	while (to < endOffset - 2 && document.get(to, 2).equals(SLASHES))
		to += 2;

	while (to < endOffset) {
		char ch= document.getChar(to);
		if (!Character.isWhitespace(ch))
			break;
		to++;
	}

	// don't count the space before javadoc like, asterix-style comment lines
	if (to > from && to < endOffset - 1 && document.get(to - 1, 2).equals(" *")) { //$NON-NLS-1$
		String type= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, to, true);
		if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT))
			to--;
	}

	return document.get(from, to - from);
}
 
Example 12
Source File: SurroundWithTemplateMenuAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isInJavadoc(JavaEditor editor) {
	ITextSelection selection= getTextSelection(editor);
	if (selection == null)
		return false;
	
	IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
	try {
		String contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, selection.getOffset(), true);
		return contentType.equals(IJavaPartitions.JAVA_DOC);
	} catch (BadLocationException e) {
		return false;
	}
}
 
Example 13
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the indentation of the line <code>line</code> in <code>document</code>.
 * The returned string may contain pairs of leading slashes that are considered
 * part of the indentation. The space before the asterisk in a javadoc-like
 * comment is not considered part of the indentation.
 *
 * @param document the document
 * @param line the line
 * @return the indentation of <code>line</code> in <code>document</code>
 * @throws BadLocationException if the document is changed concurrently
 */
private static String getCurrentIndent(Document document, int line) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int from= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// go behind line comments
	int to= from;
	while (to < endOffset - 2 && document.get(to, 2).equals(LINE_COMMENT))
		to += 2;

	while (to < endOffset) {
		char ch= document.getChar(to);
		if (!Character.isWhitespace(ch))
			break;
		to++;
	}

	// don't count the space before javadoc like, asterisk-style comment lines
	if (to > from && to < endOffset - 1 && document.get(to - 1, 2).equals(" *")) { //$NON-NLS-1$
		String type= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, to, true);
		if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT))
			to--;
	}

	return document.get(from, to - from);
}
 
Example 14
Source File: TypeScriptTemplateCompletionProposalComputer.java    From typescript.java with MIT License 5 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(TypeScriptContentAssistInvocationContext context) {
	try {
		String partition = TextUtilities.getContentType(context.getDocument(),
				IJavaScriptPartitions.JAVA_PARTITIONING, context.getInvocationOffset(), true);
		if (partition.equals(IJavaScriptPartitions.JAVA_DOC)) {
			return jsDocTemplateEngine;
		} else {
			return typeScriptTemplateEngine;
		}
	} catch (BadLocationException x) {
		return null;
	}
}
 
Example 15
Source File: JavaPairMatcher.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the character at the specified offset is a less-than sign, rather than
 * the opening angle bracket of a type parameter list.
 * 
 * @param document a document
 * @param offset an offset within the document
 * @return <code>true</code> if the character at the specified offset is a less-than sign
 * @throws BadLocationException if offset is invalid in the document
 */
private boolean isLessThanOperator(IDocument document, int offset) throws BadLocationException {
	if (offset < 0) return false;
	String contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, false);
	if (!IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
		return false;
	}
	JavaHeuristicScanner scanner= new JavaHeuristicScanner(document, IJavaPartitions.JAVA_PARTITIONING, contentType);
	return !isTypeParameterOpeningBracket(offset, document, scanner);
}
 
Example 16
Source File: JavaPairMatcher.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns true if the character at the specified offset is a greater-than sign, rather than an
 * type parameter list close angle bracket.
 * 
 * @param document a document
 * @param offset an offset within the document
 * @return true if the character at the specified offset is a greater-than sign
 * @throws BadLocationException if offset is invalid in the document
 */
private boolean isGreaterThanOperator(IDocument document, int offset) throws BadLocationException {
	if (offset < 0)
		return false;
	String contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, false);
	if (!IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
		return false;
	}
	JavaHeuristicScanner scanner= new JavaHeuristicScanner(document, IJavaPartitions.JAVA_PARTITIONING, contentType);
	return !isTypeParameterClosingBracket(offset, document, scanner);
}
 
Example 17
Source File: ScriptDebugHover.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public String getHoverInfo( ITextViewer textViewer, IRegion hoverRegion )
{
	ScriptStackFrame frame = getFrame( );
	if ( frame == null )
	{
		return null;
	}
	IDocument document = textViewer.getDocument( );
	if ( document == null )
	{
		return null;
	}
	try
	{
		String str = TextUtilities.getContentType(document, IDocumentExtension3.DEFAULT_PARTITIONING, hoverRegion.getOffset( )+1, true);
		
		String variableName = document.get( hoverRegion.getOffset( ),
				hoverRegion.getLength( ) );
		
		if (JSPartitionScanner.JS_KEYWORD.equals( str ) && !"this".equals( variableName )) //$NON-NLS-1$
		{
			return null;
		}
		ScriptValue var = ( (ScriptDebugTarget) frame.getDebugTarget( ) ).evaluate( frame,
				variableName );
		if ( var != null )
		{
			return getVariableText( var );
		}
	}
	catch ( BadLocationException e )
	{
		return null;
	}
	return null;
}
 
Example 18
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected int getLineStartPosition(final IDocument document, final String line, final int length,
		final int offset) {

	String type = IDocument.DEFAULT_CONTENT_TYPE;
	try {
		type = TextUtilities.getContentType(document, IDocumentExtension3.DEFAULT_PARTITIONING, offset, false);
	} catch (BadLocationException exception) {
		// Should not happen
	}

	int lineStartPosition = super.getLineStartPosition(document, line, length, offset);
	if (tokenTypeToPartitionTypeMapperExtension.isMultiLineComment(type)
			|| tokenTypeToPartitionTypeMapperExtension.isSingleLineComment(type)) {
		try {
			IRegion lineInformation = document.getLineInformationOfOffset(offset);
			int offsetInLine = offset - lineInformation.getOffset();
			return getCommentLineStartPosition(line, length, offsetInLine, lineStartPosition);
		} catch(BadLocationException e) {
			// Should not happen
		}
	} 
	if (type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
		if (isStartOfSingleLineComment(line, length, lineStartPosition) && !isStartOfMultiLineComment(line, length, lineStartPosition)) {
			return getTextStartPosition(line, length, lineStartPosition + 1);
		}
	}
	return lineStartPosition;
}
 
Example 19
Source File: CharacterPairMatcher.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private IRegion performMatch(IDocument doc, int caretOffset) throws BadLocationException
{
	int charOffset = Math.max(caretOffset - 1, 0);
	char prevChar = doc.getChar(charOffset);
	if (!fPairs.contains(prevChar))
	{
		// Now try to right of caret
		charOffset = caretOffset;
		caretOffset += 1;
		if (charOffset >= doc.getLength())
		{
			return null;
		}
		prevChar = doc.getChar(charOffset);
		if (!fPairs.contains(prevChar))
		{
			return null;
		}
	}

	ITypedRegion partition = getPartition(doc, charOffset);
	// FIXME if we're inside a string or comment, we should limit our search to just this particular partition!
	// Drop out if the char is inside a comment
	if (isComment(doc, partition))
	{
		return null;
	}

	boolean isForward = fPairs.isStartCharacter(prevChar);
	String contentType = partition.getType();
	if (fPairs.isAmbiguous(prevChar))
	{
		// If this is common start tag, look forward, if common end tag look backwards!
		if (CompositePartitionScanner.START_SWITCH_TAG.equals(contentType))
		{
			isForward = true;
		}
		else if (CompositePartitionScanner.END_SWITCH_TAG.equals(contentType))
		{
			isForward = false;
		}
		else
		{
			// Need to look at partition transition to tell if we're at end or beginning!
			String partitionAhead = TextUtilities.getContentType(doc, fPartitioning, charOffset + 1, false);
			String partitionBehind = TextUtilities.getContentType(doc, fPartitioning, charOffset - 1, false);
			if (contentType.equals(partitionBehind) && !contentType.equals(partitionAhead))
			{
				// End because we're transitioning out of a partition on this character
				isForward = false;
			}
			else if (isUnclosedPair(prevChar, doc, charOffset))
			{
				isForward = false;
			}
		}
	}
	fAnchor = isForward ? ICharacterPairMatcher.LEFT : ICharacterPairMatcher.RIGHT;
	int searchStartPosition = isForward ? charOffset + 1 : charOffset - 1;
	char endChar = fPairs.getMatching(prevChar);

	int endOffset = -1;
	if (isForward)
	{
		endOffset = searchForward(doc, searchStartPosition, prevChar, endChar, contentType);
	}
	else
	{
		endOffset = searchBackwards(doc, searchStartPosition, prevChar, endChar, contentType);
	}

	if (endOffset == -1)
	{
		return null;
	}
	final int adjustedOffset = isForward ? charOffset : caretOffset;
	final int adjustedEndOffset = isForward ? endOffset + 1 : endOffset;
	if (adjustedEndOffset == adjustedOffset)
	{
		return null;
	}
	return new Region(Math.min(adjustedOffset, adjustedEndOffset), Math.abs(adjustedEndOffset - adjustedOffset));
}