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

The following examples show how to use org.eclipse.jface.text.TextUtilities#endsWith() . 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: JSDocAutoIndentStrategy.java    From typescript.java with MIT License 6 votes vote down vote up
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {

		if (!isSmartMode())
			return;

		if (command.text != null) {
			if (command.length == 0) {
				String[] lineDelimiters = document.getLegalLineDelimiters();
				int index = TextUtilities.endsWith(lineDelimiters, command.text);
				if (index > -1) {
					// ends with line delimiter
					if (lineDelimiters[index].equals(command.text))
						// just the line delimiter
						indentAfterNewLine(document, command);
					return;
				}
			}

			if (command.text.equals("/")) { //$NON-NLS-1$
				indentAfterCommentEnd(document, command);
				return;
			}
		}
	}
 
Example 2
Source File: JavaDocAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {

	if (!isSmartMode())
		return;

	if (command.text != null) {
		if (command.length == 0) {
			String[] lineDelimiters= document.getLegalLineDelimiters();
			int index= TextUtilities.endsWith(lineDelimiters, command.text);
			if (index > -1) {
				// ends with line delimiter
				if (lineDelimiters[index].equals(command.text))
					// just the line delimiter
					indentAfterNewLine(document, command);
				return;
			}
		}

		if (command.text.equals("/")) { //$NON-NLS-1$
			indentAfterCommentEnd(document, command);
			return;
		}
	}
}
 
Example 3
Source File: TexEditorTools.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a length of a line.
 * @param document 	IDocument that contains the line.
 * @param command 	DocumentCommand that determines the line.
 * @param delim 	are line delimiters counted to the line length 
 * @param target 	-1 = previous line, 0 = current line, 1 = next line etc... 
 * @return 			the line length 
 */
public int getLineLength(IDocument document, DocumentCommand command, 
		boolean delim, int target) {
	int line;
	
	int length = 0;
	try{
		line = document.getLineOfOffset(command.offset) + target;
		if (line < 0 || line >= document.getNumberOfLines()){
			//line = document.getLineOfOffset(command.offset);
			return 0;
		}
		
		length = document.getLineLength(line);
		if (length == 0){
			return 0;
		}
		if (!delim){
			String txt = document.get(document.getLineOffset(line), document.getLineLength(line));
			String[] del = document.getLegalLineDelimiters();
			int cnt = TextUtilities.endsWith(del ,txt);
			if (!delim && cnt > -1){
				length = length - del[cnt].length();				
			}
		}
	}catch(BadLocationException e){
		TexlipsePlugin.log("TexEditorTools.getLineLength:",e);
	}
	return length;
}
 
Example 4
Source File: TexAutoIndentStrategy.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
    if (this.indent) {
        if (itemSetted && autoItem && command.length == 0 && command.text != null
                && TextUtilities.endsWith(document.getLegalLineDelimiters(), command.text) != -1) {
            dropItem(document, command);
        } else if (command.length == 0 && command.text != null
                &&  TextUtilities.endsWith(document.getLegalLineDelimiters(), command.text) != -1) {
            smartIndentAfterNewLine(document, command);
        } else if ("}".equals(command.text)) {
            smartIndentAfterBrace(document, command);
        } else {
            itemSetted = false;
        }
    }

    if (TexAutoIndentStrategy.hardWrap && command.length == 0 && command.text != null) {
        try {
            final String contentType = ((IDocumentExtension3) document).getContentType(
                    TexEditor.TEX_PARTITIONING, command.offset, true);
            // Do not wrap in verbatim partitions
            if (!FastLaTeXPartitionScanner.TEX_VERBATIM.equals(contentType)) {
                hlw.doWrapB(document, command, lineLength);
            }
        }
        catch (Exception e) {
            // If we cannot retrieve the current content type, do not break lines
        }
    }
}
 
Example 5
Source File: TexAutoIndentStrategy.java    From texlipse with Eclipse Public License 1.0 4 votes vote down vote up
/**
     * Performs indentation after new line is detected.
     * 
     * @param document Document where new line is detected.
     * @param command Command that represent the change of the document (new
     *            line).
     */
    private void smartIndentAfterNewLine(IDocument document, DocumentCommand command) {
        try {
            itemSetted = false;
            int commandOffset = command.offset;
            int line = document.getLineOfOffset(commandOffset);
            int lineOffset = document.getLineOffset(line);
            String startLine = document.get(lineOffset, commandOffset - lineOffset);
            //this is save
            String lineDelimiter = document.getLegalLineDelimiters()
                [TextUtilities.endsWith(document.getLegalLineDelimiters(), command.text)];
            int beginIndex;
            if ((beginIndex = LatexParserUtils.findCommand(startLine, "\\begin", 0)) != -1) {
                // test if line contains \begin and search the environment (itemize,
                // table...)
                IRegion r = LatexParserUtils.getCommandArgument(startLine, beginIndex);
                if (r == null){
                	//No environment found
                    super.customizeDocumentCommand(document, command);
                    return;
                }
                String envName = startLine.substring(r.getOffset(), r.getOffset()+r.getLength());
                StringBuilder buf = new StringBuilder(command.text);

                // get indentation of \begin
/*                String prevIndentation = this.tools.getIndentation(document, line,
                        "\\begin", this.tabWidth); // NEW*/
                String prevIndentation = getIndentation(startLine);
                
                if (Arrays.binarySearch(this.indentationItems, envName) >= 0) {
                    buf.append(prevIndentation);
                    buf.append(this.indentationString);
                } else {
                    buf.append(prevIndentation);
                }
                
                if (autoItem && (envName.equals("itemize") || envName.equals("enumerate"))) {
                    buf.append("\\item ");
                    itemSetted = true;
                    itemAtLine = document.getLineOfOffset(command.offset);
                } else if (autoItem && envName.equals("description")) {
                    buf.append("\\item[]");
                    itemSetted = true;
                    itemAtLine = document.getLineOfOffset(command.offset);
                }

                command.caretOffset = command.offset + buf.length();
                command.shiftsCaret = false;
                if (autoItem && envName.equals("description")) {
                    command.caretOffset--;
                }
                
                /*
                 * looks for the \begin-statement and inserts
                 * an equivalent \end-statement (respects \begin-indentation)
                 */
                if (needsEnd(envName, document.get(), lineOffset)){
                    buf.append(lineDelimiter);
                    buf.append(prevIndentation);
                    buf.append("\\end{" + envName + "}");
                }
                command.text = buf.toString();
                
            } else {
                if (autoItem && !itemInserted(document, command)) {
                    super.customizeDocumentCommand(document, command);
                } else {
                    super.customizeDocumentCommand(document, command);
                }
            }
        } catch (BadLocationException e) {
            TexlipsePlugin.log("TexAutoIndentStrategy:SmartIndentAfterNewLine", e);
        }
    }
 
Example 6
Source File: JsniAutoEditStrategy.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns whether or not the given text ends with one of the documents legal
 * line delimiters.
 *
 * @param d the document
 * @param txt the text
 * @return <code>true</code> if <code>txt</code> ends with one of the
 *         document's line delimiters, <code>false</code> otherwise
 */
private boolean endsWithDelimiter(IDocument d, String txt) {
  String[] delimiters = d.getLegalLineDelimiters();
  if (delimiters != null) {
    return TextUtilities.endsWith(delimiters, txt) > -1;
  }

  return false;
}