org.eclipse.jface.text.DefaultLineTracker Java Examples

The following examples show how to use org.eclipse.jface.text.DefaultLineTracker. 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: TypeScriptEditor.java    From typescript.java with MIT License 6 votes vote down vote up
@Override
protected void installTabsToSpacesConverter() {
	ISourceViewer sourceViewer = getSourceViewer();
	SourceViewerConfiguration config = getSourceViewerConfiguration();
	if (config != null && sourceViewer instanceof ITextViewerExtension7) {
		int tabWidth = config.getTabWidth(sourceViewer);
		TabsToSpacesConverter tabToSpacesConverter = new TabsToSpacesConverter();
		tabToSpacesConverter.setNumberOfSpacesPerTab(tabWidth);
		IDocumentProvider provider = getDocumentProvider();
		if (provider instanceof ICompilationUnitDocumentProvider) {
			ICompilationUnitDocumentProvider cup = (ICompilationUnitDocumentProvider) provider;
			tabToSpacesConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
		} else
			tabToSpacesConverter.setLineTracker(new DefaultLineTracker());
		((ITextViewerExtension7) sourceViewer).setTabsToSpacesConverter(tabToSpacesConverter);
		updateIndentPrefixes();
	}
}
 
Example #2
Source File: LogDocument.java    From LogViewer with Eclipse Public License 2.0 6 votes vote down vote up
public LogDocument(LogFile file, String encoding) throws SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, PartInitException {
	super();
	if (file.getEncoding() == null) {
		file.setEncoding(encoding);
	}
	this.file = file;
	this.encoding = file.getEncoding();
	this.charset = Charset.forName(file.getEncoding());
	IPreferenceStore store = LogViewerPlugin.getDefault().getPreferenceStore();
	store.addPropertyChangeListener(new PropertyChangeListener());
	backlogLines = store.getInt(ILogViewerConstants.PREF_BACKLOG);
	setTextStore(new GapTextStore(50, 300, 1f));
	setLineTracker(new DefaultLineTracker());
	completeInitialization();
	reader = new BackgroundReader(file.getType(), file.getPath(), file.getNamePattern(), charset,this);
}
 
Example #3
Source File: CodeTemplateContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
	}
}
 
Example #4
Source File: Strings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #5
Source File: CompilationUnitEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void installTabsToSpacesConverter() {
	ISourceViewer sourceViewer= getSourceViewer();
	SourceViewerConfiguration config= getSourceViewerConfiguration();
	if (config != null && sourceViewer instanceof ITextViewerExtension7) {
		int tabWidth= config.getTabWidth(sourceViewer);
		TabsToSpacesConverter tabToSpacesConverter= new TabsToSpacesConverter();
		tabToSpacesConverter.setNumberOfSpacesPerTab(tabWidth);
		IDocumentProvider provider= getDocumentProvider();
		if (provider instanceof ICompilationUnitDocumentProvider) {
			ICompilationUnitDocumentProvider cup= (ICompilationUnitDocumentProvider) provider;
			tabToSpacesConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
		} else
			tabToSpacesConverter.setLineTracker(new DefaultLineTracker());
		((ITextViewerExtension7)sourceViewer).setTabsToSpacesConverter(tabToSpacesConverter);
		updateIndentPrefixes();
	}
}
 
Example #6
Source File: Strings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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) {
		Assert.isTrue(false,"Can not happend"); //$NON-NLS-1$
		return null;
	}
}
 
Example #7
Source File: IndentManipulation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Change the indent of a, possible multiple line, code string. The given number of indent units is removed,
 * and a new indent string is added.
 * <p>The first line of the code will not be changed (It is considered to have no indent as it might start in
 * the middle of a line).</p>
 *
 * @param code the code to change the indent of
 * @param indentUnitsToRemove the number of indent units to remove from each line (except the first) of the given code
 * @param tabWidth the size of one tab in space equivalents
 * @param indentWidth the width of one indentation unit in space equivalents
 * @param newIndentString the new indent string to be added to all lines (except the first)
 * @param lineDelim the new line delimiter to be used. The returned code will contain only this line delimiter.
 * @return the newly indent code, containing only the given line delimiters.
 * @exception IllegalArgumentException if:
 * <ul>
 * <li>the given <code>indentWidth</code> is lower than zero</li>
 * <li>the given <code>tabWidth</code> is lower than zero</li>
 * <li>the given <code>code</code> is null</li>
 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
 * <li>the given <code>newIndentString</code> is null</li>
 * <li>the given <code>lineDelim</code> is null</li>
 * </ul>
 */
public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) {
	if (tabWidth < 0 || indentWidth < 0 || code == null || indentUnitsToRemove < 0 || newIndentString == null || lineDelim == null) {
		throw new IllegalArgumentException();
	}

	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++) {
			IRegion region= tracker.getLineInformation(i);
			int start= region.getOffset();
			int end= start + region.getLength();
			String line= code.substring(start, end);

			if (i == 0) {  // no indent for first line (contained in the formatted string)
				buf.append(line);
			} else { // no new line after last line
				buf.append(lineDelim);
				buf.append(newIndentString);
				if(indentWidth != 0) {
					buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth));
				} else {
					buf.append(line);
				}
			}
		}
		return buf.toString();
	} catch (BadLocationException e) {
		// can not happen
		return code;
	}
}
 
Example #8
Source File: IndentManipulation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the text edits retrieved after changing the indentation of a, possible multi-line, code string.
 *
 * <p>The given number of indent units is removed, and a new indent string is added.</p>
 * <p>The first line of the code will not be changed (It is considered to have no indent as it might start in
 * the middle of a line).</p>
 *
 * @param source The code to change the indent of
 * @param indentUnitsToRemove the number of indent units to remove from each line (except the first) of the given code
 * @param tabWidth the size of one tab in space equivalents
 * @param indentWidth the width of one indentation unit in space equivalents
 * @param newIndentString the new indent string to be added to all lines (except the first)
 * @return returns the resulting text edits
 * @exception IllegalArgumentException if:
 * <ul>
 * <li>the given <code>indentWidth</code> is lower than zero</li>
 * <li>the given <code>tabWidth</code> is lower than zero</li>
 * <li>the given <code>source</code> is null</li>
 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
 * <li>the given <code>newIndentString</code> is null</li>
 * </ul>
 */
public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) {
	if (tabWidth < 0 || indentWidth < 0 || source == null || indentUnitsToRemove < 0 || newIndentString == null) {
		throw new IllegalArgumentException();
	}

	ArrayList result= new ArrayList();
	try {
		ILineTracker tracker= new DefaultLineTracker();
		tracker.set(source);
		int nLines= tracker.getNumberOfLines();
		if (nLines == 1)
			return (ReplaceEdit[])result.toArray(new ReplaceEdit[result.size()]);
		for (int i= 1; i < nLines; i++) {
			IRegion region= tracker.getLineInformation(i);
			int offset= region.getOffset();
			String line= source.substring(offset, offset + region.getLength());
			int length= indexOfIndent(line, indentUnitsToRemove, tabWidth, indentWidth);
			if (length >= 0) {
				result.add(new ReplaceEdit(offset, length, newIndentString));
			} else {
				length= measureIndentUnits(line, tabWidth, indentWidth);
				result.add(new ReplaceEdit(offset, length, "")); //$NON-NLS-1$
			}
		}
	} catch (BadLocationException cannotHappen) {
		// can not happen
	}
	return (ReplaceEdit[])result.toArray(new ReplaceEdit[result.size()]);
}
 
Example #9
Source File: CompilationUnitDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public ILineTracker createLineTracker(Object element) {
	return new DefaultLineTracker();
}