Java Code Examples for org.eclipse.jface.text.IDocument#getLegalLineDelimiters()

The following examples show how to use org.eclipse.jface.text.IDocument#getLegalLineDelimiters() . 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: JavaTemplatesPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void updatePatternViewer(Template template) {
	if (template == null) {
		getPatternViewer().getDocument().set(""); //$NON-NLS-1$
		return ;
	}
	String contextId= template.getContextTypeId();
	TemplateContextType type= getContextTypeRegistry().getContextType(contextId);
	fTemplateProcessor.setContextType(type);

	IDocument doc= getPatternViewer().getDocument();

	String start= null;
	if ("javadoc".equals(contextId)) { //$NON-NLS-1$
		start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
	} else
		start= ""; //$NON-NLS-1$

	doc.set(start + template.getPattern());
	int startLen= start.length();
	getPatternViewer().setDocument(doc, startLen, doc.getLength() - startLen);
}
 
Example 4
Source File: EmbeddedEditorModelAccess.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public void updateModel(String prefix, String editablePart, String suffix) {
	IDocument document = this.viewer.getDocument();
	if (this.insertLineBreaks) {
		String delimiter = document.getLegalLineDelimiters()[0];
		if (document instanceof IDocumentExtension4) {
			delimiter = ((IDocumentExtension4) document).getDefaultLineDelimiter();
		}
		prefix = prefix + delimiter;
		suffix = delimiter + suffix;
	}
	String model = prefix + editablePart + suffix;
	this.viewer.setRedraw(false);
	this.viewer.getUndoManager().disconnect();
	document.set(model);
	this.viewer.setVisibleRegion(prefix.length(), editablePart.length());
	this.viewer.getUndoManager().connect(this.viewer);
	this.viewer.setRedraw(true);
}
 
Example 5
Source File: CompoundMultiLineTerminalsEditStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command)
		throws BadLocationException {
	if (command.length != 0)
		return;
	String[] lineDelimiters = document.getLegalLineDelimiters();
	int delimiterIndex = TextUtilities.startsWith(lineDelimiters, command.text);
	if (delimiterIndex != -1) {
		MultiLineTerminalsEditStrategy bestStrategy = null;
		IRegion bestStart = null;
		for(MultiLineTerminalsEditStrategy strategy: strategies) {
			IRegion candidate = strategy.findStartTerminal(document, command.offset);
			if (candidate != null) {
				if (bestStart == null || bestStart.getOffset() < candidate.getOffset()) {
					bestStrategy = strategy;
					bestStart = candidate;
				}
			}
		}
		if (bestStrategy != null) {
			bestStrategy.internalCustomizeDocumentCommand(document, command);
		}
	}
}
 
Example 6
Source File: EmbeddedEditorModelAccess.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public void updatePrefix(String prefix) {
	try {
		IDocument document = this.viewer.getDocument();
		IRegion visibleRegion = this.viewer.getVisibleRegion();
		String editablePart = document.get(visibleRegion.getOffset(), visibleRegion.getLength());
		int suffixOffset = visibleRegion.getOffset() + visibleRegion.getLength();
		String suffix = "";
		if (document.getLength() - suffixOffset > 0) {
			suffix = document.get(suffixOffset, document.getLength() - suffixOffset);
			if (this.insertLineBreaks) {
				String delimiter = document.getLegalLineDelimiters()[0];
				if (document instanceof IDocumentExtension4) {
					delimiter = ((IDocumentExtension4) document).getDefaultLineDelimiter();
				}
				suffix = suffix.substring(delimiter.length());
			}
		}
		updateModel(prefix, editablePart, suffix);
	} catch (BadLocationException e) {
		throw new RuntimeException(e);
	}
}
 
Example 7
Source File: ReplAutoEdit.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isLineDelimiter(IDocument document, DocumentCommand command) {
	if (command.length != 0) {
		return false;
	}
	String originalText = command.text;
	String[] lineDelimiters = document.getLegalLineDelimiters();
	int delimiterIndex = TextUtilities.startsWith(lineDelimiters, originalText);
	return delimiterIndex != -1 && originalText.trim().length() == 0;
}
 
Example 8
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 9
Source File: BufferedDocumentScanner.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Configures the scanner by providing access to the document range over which to scan.
 *
 * @param document the document to scan
 * @param offset the offset of the document range to scan
 * @param length the length of the document range to scan
 */
public final void setRange(IDocument document, int offset, int length) {

    fDocument= document;
    fRangeOffset= offset;
    fRangeLength= length;

    String[] delimiters= document.getLegalLineDelimiters();
    fDelimiters= new char[delimiters.length][];
    for (int i= 0; i < delimiters.length; i++)
        fDelimiters[i]= delimiters[i].toCharArray();

    updateBuffer(offset);
    fOffset= 0;
}
 
Example 10
Source File: TextSelectionUtils.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * True if text ends with a newline delimiter
 */
public static boolean endsWithNewline(IDocument document, String text) {
    String[] newlines = document.getLegalLineDelimiters();
    boolean ends = false;
    for (int i = 0; i < newlines.length; i++) {
        String delimiter = newlines[i];
        if (text.indexOf(delimiter) != -1) {
            ends = true;
        }
    }
    return ends;
}
 
Example 11
Source File: TypeScriptTemplatePreferencePage.java    From typescript.java with MIT License 5 votes vote down vote up
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();
	
	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JSDTTypeScriptUIPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);
		
		IDocument doc= viewer.getDocument();
		
		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$
		
		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}		
}
 
Example 12
Source File: BufferedDocumentScanner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Configures the scanner by providing access to the document range over which to scan.
 *
 * @param document the document to scan
 * @param offset the offset of the document range to scan
 * @param length the length of the document range to scan
 */
public final void setRange(IDocument document, int offset, int length) {

	fDocument= document;
	fRangeOffset= offset;
	fRangeLength= length;

	String[] delimiters= document.getLegalLineDelimiters();
	fDelimiters= new char[delimiters.length][];
	for (int i= 0; i < delimiters.length; i++)
		fDelimiters[i]= delimiters[i].toCharArray();

	updateBuffer(offset);
	fOffset= 0;
}
 
Example 13
Source File: BufferedDocumentScanner.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Configures the scanner by providing access to the document range over
 * which to scan.
 * 
 * @param document
 *            the document to scan
 * @param offset
 *            the offset of the document range to scan
 * @param length
 *            the length of the document range to scan
 */
public final void setRange(IDocument document, int offset, int length) {

	fDocument = document;
	fRangeOffset = offset;
	fRangeLength = length;

	String[] delimiters = document.getLegalLineDelimiters();
	fDelimiters = new char[delimiters.length][];
	for (int i = 0; i < delimiters.length; i++)
		fDelimiters[i] = delimiters[i].toCharArray();

	updateBuffer(offset);
	fOffset = 0;
}
 
Example 14
Source File: CommonAutoIndentStrategy.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
protected static boolean isLineDelimiter(IDocument d, String text)
{
	String[] delimiters = d.getLegalLineDelimiters();
	if (delimiters == null)
	{
		return false;
	}
	return TextUtilities.equals(delimiters, text) > -1;
}
 
Example 15
Source File: MultiLineTerminalsEditStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isLineDelimiter(IDocument document, DocumentCommand command) {
	if (command.length != 0) {
		return false;
	}
	String originalText = command.text;
	String[] lineDelimiters = document.getLegalLineDelimiters();
	int delimiterIndex = TextUtilities.startsWith(lineDelimiters, originalText);
	return delimiterIndex != -1 && originalText.trim().length() == 0;
}
 
Example 16
Source File: ChangeHoverInformationControl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void setInformation(String content) {
	super.setInformation(content);
	IDocument doc= getViewer().getDocument();
	if (doc == null)
		return;

	// ensure that we can scroll enough
	ensureScrollable();

	String start= null;
	if (IJavaPartitions.JAVA_DOC.equals(fPartition)) {
		start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
	} else if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(fPartition)) {
		start= "/*" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
	}
	if (start != null) {
		try {
			doc.replace(0, 0, start);
			int startLen= start.length();
			getViewer().setDocument(doc, startLen, doc.getLength() - startLen);
		} catch (BadLocationException e) {
			// impossible
			Assert.isTrue(false);
		}
	}

	getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel);
}
 
Example 17
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 18
Source File: TypeScriptAutoIndentStrategy.java    From typescript.java with MIT License 4 votes vote down vote up
private boolean isLineDelimiter(IDocument document, String text) {
	String[] delimiters= document.getLegalLineDelimiters();
	if (delimiters != null)
		return TextUtilities.equals(delimiters, text) > -1;
	return false;
}
 
Example 19
Source File: JavaStringAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isLineDelimiter(IDocument document, String text) {
	String[] delimiters= document.getLegalLineDelimiters();
	if (delimiters != null)
		return TextUtilities.equals(delimiters, text) > -1;
	return false;
}
 
Example 20
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isLineDelimiter(IDocument document, String text) {
	String[] delimiters= document.getLegalLineDelimiters();
	if (delimiters != null)
		return TextUtilities.equals(delimiters, text) > -1;
	return false;
}