Search in sources :

Example 1 with DefaultLineTracker

use of org.eclipse.jface.text.DefaultLineTracker in project che by eclipse.

the class CodeTemplateContext method changeLineDelimiter.

private static String changeLineDelimiter(String code, String lineDelim) {
    try {
        ILineTracker tracker = new DefaultLineTracker();
        tracker.set(code);
        int nLines = tracker.getNumberOfLines();
        if (nLines == 1) {
            return code;
        }
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < nLines; i++) {
            if (i != 0) {
                buf.append(lineDelim);
            }
            IRegion region = tracker.getLineInformation(i);
            String line = code.substring(region.getOffset(), region.getOffset() + region.getLength());
            buf.append(line);
        }
        return buf.toString();
    } catch (BadLocationException e) {
        // can not happen
        return code;
    }
}
Also used : DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ILineTracker(org.eclipse.jface.text.ILineTracker)

Example 2 with DefaultLineTracker

use of org.eclipse.jface.text.DefaultLineTracker in project che by eclipse.

the class Strings method convertIntoLines.

/**
	 * Converts the given string into an array of lines. The lines
	 * don't contain any line delimiter characters.
	 *
	 * @param input the string
	 * @return the string converted into an array of strings. Returns <code>
	 * 	null</code> if the input string can't be converted in an array of lines.
	 */
public static String[] convertIntoLines(String input) {
    try {
        ILineTracker tracker = new DefaultLineTracker();
        tracker.set(input);
        int size = tracker.getNumberOfLines();
        String[] result = new String[size];
        for (int i = 0; i < size; i++) {
            IRegion region = tracker.getLineInformation(i);
            int offset = region.getOffset();
            result[i] = input.substring(offset, offset + region.getLength());
        }
        return result;
    } catch (BadLocationException e) {
        return null;
    }
}
Also used : DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) StyledString(org.eclipse.jface.viewers.StyledString) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ILineTracker(org.eclipse.jface.text.ILineTracker)

Example 3 with DefaultLineTracker

use of org.eclipse.jface.text.DefaultLineTracker in project che by eclipse.

the class Strings method trimIndentation.

public static String trimIndentation(String source, int tabWidth, int indentWidth, boolean considerFirstLine) {
    try {
        ILineTracker tracker = new DefaultLineTracker();
        tracker.set(source);
        int size = tracker.getNumberOfLines();
        if (size == 1)
            return source;
        String[] lines = new String[size];
        for (int i = 0; i < size; i++) {
            IRegion region = tracker.getLineInformation(i);
            int offset = region.getOffset();
            lines[i] = source.substring(offset, offset + region.getLength());
        }
        Strings.trimIndentation(lines, tabWidth, indentWidth, considerFirstLine);
        StringBuffer result = new StringBuffer();
        int last = size - 1;
        for (int i = 0; i < size; i++) {
            result.append(lines[i]);
            if (i < last)
                result.append(tracker.getLineDelimiter(i));
        }
        return result.toString();
    } catch (BadLocationException e) {
        //$NON-NLS-1$
        Assert.isTrue(false, "Can not happend");
        return null;
    }
}
Also used : DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) StyledString(org.eclipse.jface.viewers.StyledString) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ILineTracker(org.eclipse.jface.text.ILineTracker)

Example 4 with DefaultLineTracker

use of org.eclipse.jface.text.DefaultLineTracker in project che by eclipse.

the class DocumentAdapter method validateLineDelimiters.

private void validateLineDelimiters(String contents) {
    if (fLegalLineDelimiters == null) {
        // collect all line delimiters in the document
        HashSet<String> existingDelimiters = new HashSet<String>();
        for (int i = fDocument.getNumberOfLines() - 1; i >= 0; i--) {
            try {
                String curr = fDocument.getLineDelimiter(i);
                if (curr != null) {
                    existingDelimiters.add(curr);
                }
            } catch (BadLocationException e) {
                JavaPlugin.log(e);
            }
        }
        if (existingDelimiters.isEmpty()) {
            // first insertion of a line delimiter: no test
            return;
        }
        fLegalLineDelimiters = existingDelimiters;
    }
    DefaultLineTracker tracker = new DefaultLineTracker();
    tracker.set(contents);
    int lines = tracker.getNumberOfLines();
    if (lines <= 1)
        return;
    for (int i = 0; i < lines; i++) {
        try {
            String curr = tracker.getLineDelimiter(i);
            if (curr != null && !fLegalLineDelimiters.contains(curr)) {
                //$NON-NLS-1$
                StringBuffer buf = new StringBuffer("WARNING: javaeditor.DocumentAdapter added new line delimiter to code: ");
                for (int k = 0; k < curr.length(); k++) {
                    if (k > 0)
                        buf.append(' ');
                    buf.append((int) curr.charAt(k));
                }
                IStatus status = new Status(IStatus.WARNING, JavaPlugin.ID_PLUGIN, IStatus.OK, buf.toString(), new Throwable());
                JavaPlugin.log(status);
            }
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) BadLocationException(org.eclipse.jface.text.BadLocationException) HashSet(java.util.HashSet)

Example 5 with DefaultLineTracker

use of org.eclipse.jface.text.DefaultLineTracker in project flux by eclipse.

the class Strings method trimIndentation.

public static String trimIndentation(String source, int tabWidth, int indentWidth, boolean considerFirstLine) {
    try {
        ILineTracker tracker = new DefaultLineTracker();
        tracker.set(source);
        int size = tracker.getNumberOfLines();
        if (size == 1)
            return source;
        String[] lines = new String[size];
        for (int i = 0; i < size; i++) {
            IRegion region = tracker.getLineInformation(i);
            int offset = region.getOffset();
            lines[i] = source.substring(offset, offset + region.getLength());
        }
        Strings.trimIndentation(lines, tabWidth, indentWidth, considerFirstLine);
        StringBuffer result = new StringBuffer();
        int last = size - 1;
        for (int i = 0; i < size; i++) {
            result.append(lines[i]);
            if (i < last)
                result.append(tracker.getLineDelimiter(i));
        }
        return result.toString();
    } catch (BadLocationException e) {
        //$NON-NLS-1$
        Assert.isTrue(false, "Can not happend");
        return null;
    }
}
Also used : DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) StyledString(org.eclipse.jface.viewers.StyledString) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ILineTracker(org.eclipse.jface.text.ILineTracker)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)7 DefaultLineTracker (org.eclipse.jface.text.DefaultLineTracker)7 ILineTracker (org.eclipse.jface.text.ILineTracker)6 IRegion (org.eclipse.jface.text.IRegion)6 StyledString (org.eclipse.jface.viewers.StyledString)4 HashSet (java.util.HashSet)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1