mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Removed unused package org.eclipse.cdt.internal corext.textmanipulation, functionality is covered by the platform.
This commit is contained in:
parent
074c73c018
commit
07bfa75108
8 changed files with 0 additions and 1076 deletions
|
@ -1,73 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2004, 2006 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Rational Software - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.runtime.Assert;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
import org.eclipse.text.edits.TextEdit;
|
||||
|
||||
public class GroupDescription {
|
||||
|
||||
private String fDescription;
|
||||
private List fEdits;
|
||||
|
||||
public GroupDescription() {
|
||||
this( "NO_DESCRIPTION"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
public GroupDescription(String description) {
|
||||
super();
|
||||
Assert.isNotNull(description);
|
||||
fDescription= description;
|
||||
fEdits= new ArrayList(3);
|
||||
}
|
||||
|
||||
public GroupDescription(String description, TextEdit[] edits) {
|
||||
super();
|
||||
Assert.isNotNull(description);
|
||||
Assert.isNotNull(edits);
|
||||
fDescription= description;
|
||||
fEdits= new ArrayList(Arrays.asList(edits));
|
||||
}
|
||||
|
||||
public void addTextEdit(TextEdit edit) {
|
||||
fEdits.add(edit);
|
||||
}
|
||||
|
||||
public boolean hasTextEdits() {
|
||||
return fEdits.isEmpty();
|
||||
}
|
||||
|
||||
public TextEdit[] getTextEdits() {
|
||||
return (TextEdit[]) fEdits.toArray(new TextEdit[fEdits.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text range covered by the edits managed via this
|
||||
* group description. The method requires that the group description
|
||||
* manages at least one text edit.
|
||||
*/
|
||||
public IRegion getTextRange() {
|
||||
int size= fEdits.size();
|
||||
if (size == 1) {
|
||||
return ((TextEdit)fEdits.get(0)).getRegion();
|
||||
}
|
||||
return TextEdit.getCoverage((TextEdit[])fEdits.toArray(new TextEdit[fEdits.size()]));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return fDescription;
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2005 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
* QNX Software System
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
/**
|
||||
* A source range defines an element's source coordinates relative to
|
||||
* its source buffer.
|
||||
* <p>
|
||||
* This interface is not intended to be implemented by clients.
|
||||
* </p>
|
||||
*/
|
||||
public interface ISourceRange {
|
||||
|
||||
/**
|
||||
* Returns the number of characters of the source code for this element,
|
||||
* relative to the source buffer in which this element is contained.
|
||||
*/
|
||||
int getLength();
|
||||
/**
|
||||
* Returns the 0-based index of the first character of the source code for this element,
|
||||
* relative to the source buffer in which this element is contained.
|
||||
*/
|
||||
int getOffset();
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
################################################################################
|
||||
# Copyright (c) 2002, 2005 IBM Corporation and others.
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# which accompanies this distribution, and is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
#
|
||||
# Contributors:
|
||||
# IBM Corporation - initial API and implementation
|
||||
# QnX Software System
|
||||
################################################################################
|
||||
|
||||
TextRange.offset=Offset:
|
||||
TextRange.length=\ Length:
|
||||
|
||||
TextBuffer.wrongRange=Replace failed due to wrong range [{0},{1}]
|
||||
|
||||
TextBufferFactory.bufferNotManaged=Buffer not managed
|
||||
TextBufferEditor.rootNodeEmpty=RootNode empty
|
|
@ -1,461 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2005 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
* QNX Software System
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.DefaultLineTracker;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IDocumentListener;
|
||||
import org.eclipse.jface.text.ILineTracker;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
import org.eclipse.jface.util.Assert;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.CStatusConstants;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of a <code>TextBuffer</code> that is based on <code>ITextSelection</code>
|
||||
* and <code>IDocument</code>.
|
||||
*/
|
||||
public class TextBuffer {
|
||||
|
||||
private static class DocumentRegion extends TextRegion {
|
||||
IRegion fRegion;
|
||||
public DocumentRegion(IRegion region) {
|
||||
fRegion= region;
|
||||
}
|
||||
public int getOffset() {
|
||||
return fRegion.getOffset();
|
||||
}
|
||||
public int getLength() {
|
||||
return fRegion.getLength();
|
||||
}
|
||||
}
|
||||
|
||||
private IDocument fDocument;
|
||||
|
||||
public IDocument getDocument() {
|
||||
return fDocument;
|
||||
}
|
||||
|
||||
private static final TextBufferFactory fgFactory= new TextBufferFactory();
|
||||
|
||||
TextBuffer(IDocument document) {
|
||||
fDocument= document;
|
||||
Assert.isNotNull(fDocument);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of characters in this text buffer.
|
||||
*
|
||||
* @return the number of characters in this text buffer
|
||||
*/
|
||||
public int getLength() {
|
||||
return fDocument.getLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of lines in this text buffer.
|
||||
*
|
||||
* @return the number of lines in this text buffer
|
||||
*/
|
||||
public int getNumberOfLines() {
|
||||
return fDocument.getNumberOfLines();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character at the given offset in this text buffer.
|
||||
*
|
||||
* @param offset a text buffer offset
|
||||
* @return the character at the offset
|
||||
* @exception IndexOutOfBoundsException if the <code>offset</code>
|
||||
* argument is negative or not less than the length of this text buffer.
|
||||
*/
|
||||
public char getChar(int offset) {
|
||||
try {
|
||||
return fDocument.getChar(offset);
|
||||
} catch (BadLocationException e) {
|
||||
throw new ArrayIndexOutOfBoundsException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the whole content of the text buffer.
|
||||
*
|
||||
* @return the whole content of the text buffer
|
||||
*/
|
||||
public String getContent() {
|
||||
return fDocument.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns length characters starting from the specified position.
|
||||
*
|
||||
* @return the characters specified by the given text region. Returns <code>
|
||||
* null</code> if text range is illegal
|
||||
*/
|
||||
public String getContent(int start, int length) {
|
||||
try {
|
||||
return fDocument.get(start, length);
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the preferred line delimiter to be used for this text buffer.
|
||||
*
|
||||
* @return the preferred line delimiter
|
||||
*/
|
||||
public String getLineDelimiter() {
|
||||
String lineDelimiter= getLineDelimiter(0);
|
||||
if (lineDelimiter == null)
|
||||
lineDelimiter= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
return lineDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line delimiter used for the given line number. Returns <code>
|
||||
* null</code> if the line number is out of range.
|
||||
*
|
||||
* @return the line delimiter used by the given line number or <code>null</code>
|
||||
*/
|
||||
public String getLineDelimiter(int line) {
|
||||
try {
|
||||
return fDocument.getLineDelimiter(line);
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line for the given line number. If there isn't any line for
|
||||
* the given line number, <code>null</code> is returned.
|
||||
*
|
||||
* @return the line for the given line number or <code>null</code>
|
||||
*/
|
||||
public String getLineContent(int line) {
|
||||
try {
|
||||
IRegion region= fDocument.getLineInformation(line);
|
||||
return fDocument.get(region.getOffset(), region.getLength());
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line indent for the given line. If there isn't any line for the
|
||||
* given line number, <code>-1</code> is returned.
|
||||
*
|
||||
* @return the line indent for the given line number of <code>-1</code>
|
||||
*/
|
||||
public int getLineIndent(int lineNumber, int tabWidth) {
|
||||
return getIndent(getLineContent(lineNumber), tabWidth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a region of the specified line. The region contains the offset and the
|
||||
* length of the line excluding the line's delimiter. Returns <code>null</code>
|
||||
* if the line doesn't exist.
|
||||
*
|
||||
* @param line the line of interest
|
||||
* @return a line description or <code>null</code> if the given line doesn't
|
||||
* exist
|
||||
*/
|
||||
public TextRegion getLineInformation(int line) {
|
||||
try {
|
||||
return new DocumentRegion(fDocument.getLineInformation(line));
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a line region of the specified offset. The region contains the offset and
|
||||
* the length of the line excluding the line's delimiter. Returns <code>null</code>
|
||||
* if the line doesn't exist.
|
||||
*
|
||||
* @param offset an offset into a line
|
||||
* @return a line description or <code>null</code> if the given line doesn't
|
||||
* exist
|
||||
*/
|
||||
public TextRegion getLineInformationOfOffset(int offset) {
|
||||
try {
|
||||
return new DocumentRegion(fDocument.getLineInformationOfOffset(offset));
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line number that contains the given position. If there isn't any
|
||||
* line that contains the position, <code>null</code> is returned. The returned
|
||||
* string is a copy and doesn't contain the line delimiter.
|
||||
*
|
||||
* @return the line that contains the given offset or <code>null</code> if line
|
||||
* doesn't exist
|
||||
*/
|
||||
public int getLineOfOffset(int offset) {
|
||||
try {
|
||||
return fDocument.getLineOfOffset(offset);
|
||||
} catch (BadLocationException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line that contains the given position. If there isn't any
|
||||
* line that contains the position, <code>null</code> is returned. The returned
|
||||
* string is a copy and doesn't contain the line delimiter.
|
||||
*
|
||||
* @return the line that contains the given offset or <code>null</code> if line
|
||||
* doesn't exist
|
||||
*/
|
||||
public String getLineContentOfOffset(int offset) {
|
||||
try {
|
||||
IRegion region= fDocument.getLineInformationOfOffset(offset);
|
||||
return fDocument.get(region.getOffset(), region.getLength());
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the text determined by the region [offset, length] into an array of lines.
|
||||
* The lines are copies of the original lines and don't contain any line delimiter
|
||||
* characters.
|
||||
*
|
||||
* @return the text converted into an array of strings. Returns <code>null</code> if the
|
||||
* region lies outside the source.
|
||||
*/
|
||||
public String[] convertIntoLines(int offset, int length) {
|
||||
try {
|
||||
String text= fDocument.get(offset, length);
|
||||
ILineTracker tracker= new DefaultLineTracker();
|
||||
tracker.set(text);
|
||||
int size= tracker.getNumberOfLines();
|
||||
String result[]= new String[size];
|
||||
for (int i= 0; i < size; i++) {
|
||||
IRegion region= tracker.getLineInformation(i);
|
||||
result[i]= getContent(offset + region.getOffset(), region.getLength());
|
||||
}
|
||||
return result;
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subsitutes the given text for the specified text position
|
||||
*
|
||||
* @param offset the starting offset of the text to be replaced
|
||||
* @param length the length of the text to be replaced
|
||||
* @param text the substitution text
|
||||
* @exception CoreException if the text position [offset, length] is invalid.
|
||||
*/
|
||||
public void replace(int offset, int length, String text) throws CoreException {
|
||||
try {
|
||||
fDocument.replace(offset, length, text);
|
||||
} catch (BadLocationException e) {
|
||||
IStatus s= new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, CStatusConstants.INTERNAL_ERROR,
|
||||
TextManipulationMessages.getFormattedString(
|
||||
"TextBuffer.wrongRange", //$NON-NLS-1$
|
||||
new Object[] {new Integer(offset), new Integer(length) } ), e);
|
||||
throw new CoreException(s);
|
||||
}
|
||||
}
|
||||
|
||||
public void replace(TextRange range, String text) throws CoreException {
|
||||
replace(range.fOffset, range.fLength, text);
|
||||
}
|
||||
|
||||
//---- Special methods used by the <code>TextBufferEditor</code>
|
||||
|
||||
/**
|
||||
* Releases this text buffer.
|
||||
*/
|
||||
/* package */ void release() {
|
||||
}
|
||||
|
||||
/* package */ void registerUpdater(IDocumentListener listener) {
|
||||
fDocument.addDocumentListener(listener);
|
||||
}
|
||||
|
||||
/* package */ void unregisterUpdater(IDocumentListener listener) {
|
||||
fDocument.removeDocumentListener(listener);
|
||||
}
|
||||
|
||||
//---- Utility methods
|
||||
|
||||
/**
|
||||
* Returns the indent for the given line.
|
||||
* If line is <code>null</code>, <code>-1</code> is returned.
|
||||
*
|
||||
* @param line the line for which the indent is determined
|
||||
* @return the line indent for the given line number or <code>-1</code>
|
||||
*/
|
||||
public static int getIndent(String line, int tabWidth) {
|
||||
if (line == null)
|
||||
return -1;
|
||||
int indent= 0;
|
||||
int blanks= 0;
|
||||
int size= line.length();
|
||||
for (int i= 0; i < size; i++) {
|
||||
switch (line.charAt(i)) {
|
||||
case '\t':
|
||||
indent++;
|
||||
blanks= 0;
|
||||
continue;
|
||||
case ' ':
|
||||
blanks++;
|
||||
if (blanks == tabWidth) {
|
||||
indent++;
|
||||
blanks= 0;
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return indent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the line with the given number of identations
|
||||
* removed from the beginning.
|
||||
* If the count is zero, the line is returned.
|
||||
*/
|
||||
public static String removeIndent(String line, int indentsToRemove, int tabWidth) {
|
||||
if (line != null) {
|
||||
int indent= 0;
|
||||
int blanks= 0;
|
||||
int size= line.length();
|
||||
for (int i= 0; i < size; i++) {
|
||||
|
||||
if (indent >= indentsToRemove) {
|
||||
line= line.substring(i);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (line.charAt(i)) {
|
||||
case '\t':
|
||||
indent++;
|
||||
blanks= 0;
|
||||
continue;
|
||||
case ' ':
|
||||
blanks++;
|
||||
if (blanks == tabWidth) {
|
||||
indent++;
|
||||
blanks= 0;
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
//---- Factory methods ----------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Acquires a text buffer for the given file. If a text buffer for the given
|
||||
* file already exists, then that one is returned.
|
||||
*
|
||||
* @param file the file for which a text buffer is requested
|
||||
* @return a managed text buffer for the given file
|
||||
* @exception CoreException if it was not possible to acquire the
|
||||
* text buffer
|
||||
*/
|
||||
public static TextBuffer acquire(IFile file) throws CoreException {
|
||||
return fgFactory.acquire(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the given text buffer.
|
||||
*
|
||||
* @param buffer the text buffer to be released
|
||||
*/
|
||||
public static void release(TextBuffer buffer) {
|
||||
fgFactory.release(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits the changes made to the given text buffer to the underlying
|
||||
* storage system.
|
||||
*
|
||||
* @param buffer the text buffer containing the changes to be committed.
|
||||
* @param force if <code>true</code> the text buffer is committed in any case.
|
||||
* If <code>false</code> the text buffer is <b>ONLY</b> committed if the client
|
||||
* is the last one that holds a reference to the text buffer. Clients of this
|
||||
* method must make sure that they don't call this method from within an <code>
|
||||
* IWorkspaceRunnable</code>.
|
||||
* @param pm the progress monitor used to report progress if committing is
|
||||
* necessary
|
||||
*/
|
||||
public static void commitChanges(TextBuffer buffer, boolean force, IProgressMonitor pm) throws CoreException {
|
||||
fgFactory.commitChanges(buffer, force, pm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>TextBuffer</code> for the given file. The returned
|
||||
* buffer will not be managed. Any subsequent call to <code>create</code>
|
||||
* with the same file will return a different text buffer.
|
||||
* <p>
|
||||
* If the file is currently open in a text editor, the editors content is copied into
|
||||
* the returned <code>TextBuffer</code>. Otherwise the content is read from
|
||||
* disk.
|
||||
*
|
||||
* @param file the file for which a text buffer is to be created
|
||||
* @return a new unmanaged text buffer
|
||||
* @exception CoreException if it was not possible to create the text buffer
|
||||
*/
|
||||
public static TextBuffer create(IFile file) throws CoreException {
|
||||
return fgFactory.create(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>TextBuffer</code> for the string. The returned
|
||||
* buffer will not be managed. Any subsequent call to <code>create</code>
|
||||
* with the same string will return a different text buffer.
|
||||
*
|
||||
* @param content the text buffer's content
|
||||
* @return a new unmanaged text buffer
|
||||
* @exception CoreException if it was not possible to create the text buffer
|
||||
*/
|
||||
public static TextBuffer create(String content) throws CoreException {
|
||||
return fgFactory.create(content);
|
||||
}
|
||||
|
||||
// Unclear which methods are needed if we get the new save model. If optimal no
|
||||
// save is needed at all.
|
||||
|
||||
public static void save(TextBuffer buffer, IProgressMonitor pm) throws CoreException {
|
||||
fgFactory.save(buffer, pm);
|
||||
}
|
||||
|
||||
public static void aboutToChange(TextBuffer buffer) throws CoreException {
|
||||
fgFactory.aboutToChange(buffer);
|
||||
}
|
||||
|
||||
public static void changed(TextBuffer buffer) throws CoreException {
|
||||
fgFactory.changed(buffer);
|
||||
}
|
||||
}
|
|
@ -1,194 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2005 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
* QNX Software System
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.CStatusConstants;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IWorkspaceRunnable;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.jface.text.Document;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.source.IAnnotationModel;
|
||||
import org.eclipse.jface.util.Assert;
|
||||
import org.eclipse.ui.part.FileEditorInput;
|
||||
import org.eclipse.ui.texteditor.IDocumentProvider;
|
||||
|
||||
|
||||
/* package */ class TextBufferFactory {
|
||||
|
||||
protected IDocumentProvider fDocumentProvider;
|
||||
private Map fFileValueMap;
|
||||
private Map fBufferValueMap;
|
||||
|
||||
private static class Value {
|
||||
TextBuffer buffer;
|
||||
FileEditorInput input;
|
||||
IDocument document;
|
||||
IAnnotationModel annotationModel;
|
||||
int references;
|
||||
public Value(TextBuffer b, FileEditorInput i, IDocument d, IAnnotationModel m) {
|
||||
buffer= b;
|
||||
input= i;
|
||||
document= d;
|
||||
annotationModel= m;
|
||||
}
|
||||
}
|
||||
|
||||
public TextBufferFactory() {
|
||||
// XXX http://dev.eclipse.org/bugs/show_bug.cgi?id=5170
|
||||
// Need way to map a file to a document without knowing any kind of document provider.
|
||||
this(CUIPlugin.getDefault().getDocumentProvider());
|
||||
}
|
||||
|
||||
public TextBufferFactory(IDocumentProvider provider) {
|
||||
fDocumentProvider= provider;
|
||||
Assert.isNotNull(fDocumentProvider);
|
||||
fFileValueMap= new HashMap(5);
|
||||
fBufferValueMap= new HashMap(5);
|
||||
}
|
||||
|
||||
public TextBuffer acquire(IFile file) throws CoreException {
|
||||
FileEditorInput input= new FileEditorInput(file);
|
||||
|
||||
Value value= (Value)fFileValueMap.get(input);
|
||||
if (value != null) {
|
||||
value.references++;
|
||||
return value.buffer;
|
||||
}
|
||||
|
||||
fDocumentProvider.connect(input);
|
||||
IDocument document= fDocumentProvider.getDocument(input);
|
||||
IAnnotationModel annotationModel= fDocumentProvider.getAnnotationModel(input);
|
||||
annotationModel.connect(document);
|
||||
value= new Value(new TextBuffer(document), input, document, annotationModel);
|
||||
fFileValueMap.put(input, value);
|
||||
fBufferValueMap.put(value.buffer, value);
|
||||
value.references++;
|
||||
return value.buffer;
|
||||
}
|
||||
|
||||
public void release(TextBuffer buffer) {
|
||||
final Value value= (Value)fBufferValueMap.get(buffer);
|
||||
if (value == null)
|
||||
return;
|
||||
|
||||
value.references--;
|
||||
if (value.references == 0) {
|
||||
buffer.release();
|
||||
value.annotationModel.disconnect(value.document);
|
||||
fDocumentProvider.disconnect(value.input);
|
||||
fFileValueMap.remove(value.input);
|
||||
fBufferValueMap.remove(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void commitChanges(TextBuffer buffer, boolean force, IProgressMonitor pm) throws CoreException {
|
||||
final Value value= (Value)fBufferValueMap.get(buffer);
|
||||
if (value == null)
|
||||
return;
|
||||
|
||||
boolean save= force || fDocumentProvider.mustSaveDocument(value.input);
|
||||
if (save) {
|
||||
IWorkspaceRunnable action= new IWorkspaceRunnable() {
|
||||
public void run(IProgressMonitor pm) throws CoreException {
|
||||
fDocumentProvider.aboutToChange(value.input);
|
||||
fDocumentProvider.saveDocument(pm, value.input, value.document, true);
|
||||
}
|
||||
};
|
||||
try {
|
||||
ResourcesPlugin.getWorkspace().run(action, pm);
|
||||
} finally {
|
||||
fDocumentProvider.changed(value.input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TextBuffer create(IFile file) throws CoreException {
|
||||
FileEditorInput input= new FileEditorInput(file);
|
||||
IDocument document= fDocumentProvider.getDocument(input);
|
||||
if (document != null) {
|
||||
return new TextBuffer(new Document(document.get()));
|
||||
}
|
||||
return createFromFile(file);
|
||||
}
|
||||
|
||||
private TextBuffer createFromFile(IFile file) throws CoreException {
|
||||
IDocument document;
|
||||
InputStreamReader in= null;
|
||||
try {
|
||||
document= new Document();
|
||||
in= new InputStreamReader(new BufferedInputStream(file.getContents()));
|
||||
StringBuffer buffer= new StringBuffer();
|
||||
char[] readBuffer= new char[2048];
|
||||
int n= in.read(readBuffer);
|
||||
while (n > 0) {
|
||||
buffer.append(readBuffer, 0, n);
|
||||
n= in.read(readBuffer);
|
||||
}
|
||||
document.set(buffer.toString());
|
||||
return new TextBuffer(document);
|
||||
} catch (IOException x) {
|
||||
IStatus s= new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, CStatusConstants.INTERNAL_ERROR, x.getMessage(), x);
|
||||
throw new CoreException(s);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException x) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TextBuffer create(String content) throws CoreException {
|
||||
return new TextBuffer(new Document(content));
|
||||
}
|
||||
|
||||
public void save(TextBuffer buffer, IProgressMonitor pm) throws CoreException {
|
||||
Value value= (Value)fBufferValueMap.get(buffer);
|
||||
if (value == null)
|
||||
throwNotManaged();
|
||||
fDocumentProvider.saveDocument(pm, value.input, value.document, true);
|
||||
}
|
||||
|
||||
public void aboutToChange(TextBuffer buffer) throws CoreException {
|
||||
Value value= (Value)fBufferValueMap.get(buffer);
|
||||
if (value == null)
|
||||
throwNotManaged();
|
||||
fDocumentProvider.aboutToChange(value.input);
|
||||
}
|
||||
|
||||
public void changed(TextBuffer buffer) throws CoreException {
|
||||
Value value= (Value)fBufferValueMap.get(buffer);
|
||||
if (value == null)
|
||||
throwNotManaged();
|
||||
fDocumentProvider.changed(value.input);
|
||||
}
|
||||
|
||||
private void throwNotManaged() throws CoreException {
|
||||
IStatus s= new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID,
|
||||
CStatusConstants.INTERNAL_ERROR, "TextBufferFactory.bufferNotManaged", null); //$NON-NLS-1$
|
||||
throw new CoreException(s);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2002, 2005 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class TextManipulationMessages {
|
||||
|
||||
private static final String BUNDLE_NAME= "org.eclipse.cdt.internal.corext.textmanipulation.Messages"; //$NON-NLS-1$
|
||||
|
||||
private static ResourceBundle fgResourceBundle;
|
||||
static {
|
||||
try {
|
||||
fgResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
|
||||
} catch (MissingResourceException x) {
|
||||
fgResourceBundle = null;
|
||||
}
|
||||
}
|
||||
|
||||
private TextManipulationMessages() {
|
||||
}
|
||||
|
||||
public static String getString(String key) {
|
||||
try {
|
||||
return fgResourceBundle.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
return '!' + key + '!';
|
||||
} catch (NullPointerException e) {
|
||||
return "#" + key + "#"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFormattedString(String key, Object arg) {
|
||||
return getFormattedString(key, new Object[] { arg });
|
||||
}
|
||||
|
||||
public static String getFormattedString(String key, Object[] args) {
|
||||
return MessageFormat.format(getString(key), args);
|
||||
}
|
||||
}
|
|
@ -1,202 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2005 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
* QNX Software System
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
import org.eclipse.jface.util.Assert;
|
||||
|
||||
|
||||
|
||||
public final class TextRange {
|
||||
|
||||
/* package */ int fOffset;
|
||||
/* package */ int fLength;
|
||||
|
||||
public static final TextRange UNDEFINED= new TextRange((TextRange)null);
|
||||
|
||||
/**
|
||||
* Creates a insert position with the given offset.
|
||||
*
|
||||
* @param offset the position offset, must be >= 0
|
||||
*/
|
||||
public TextRange(int offset) {
|
||||
this(offset, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new range with the given offset and length.
|
||||
*
|
||||
* @param offset the position offset, must be >= 0
|
||||
* @param length the position length, must be >= 0
|
||||
*/
|
||||
public TextRange(int offset, int length) {
|
||||
fOffset= offset;
|
||||
Assert.isTrue(fOffset >= 0);
|
||||
fLength= length;
|
||||
Assert.isTrue(fLength >= 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the undefined text range.
|
||||
*/
|
||||
private TextRange(TextRange dummy) {
|
||||
fOffset= -1;
|
||||
fLength= -1;
|
||||
}
|
||||
|
||||
public static TextRange createFromStartAndLength(int start, int length) {
|
||||
return new TextRange(start, length);
|
||||
}
|
||||
|
||||
public static TextRange createFromStartAndInclusiveEnd(int start, int end) {
|
||||
return new TextRange(start, end - start + 1);
|
||||
}
|
||||
|
||||
public static TextRange createFromStartAndExclusiveEnd(int start, int end) {
|
||||
return new TextRange(start, end - start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new range from the given source range.
|
||||
*
|
||||
* @range the source range denoting offset and length
|
||||
*/
|
||||
public TextRange(ISourceRange range) {
|
||||
this(range.getOffset(), range.getLength());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset of this range.
|
||||
*
|
||||
* @return the length of this range
|
||||
*/
|
||||
public int getOffset() {
|
||||
return fOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of this range.
|
||||
*
|
||||
* @return the length of this range
|
||||
*/
|
||||
public int getLength() {
|
||||
return fLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the inclusive end position of this range. That means that the end position
|
||||
* denotes the last character of this range.
|
||||
*
|
||||
* @return the inclusive end position
|
||||
*/
|
||||
public int getInclusiveEnd() {
|
||||
return fOffset + fLength - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the exclusive end position of this range. That means that the end position
|
||||
* denotes the first character after this range.
|
||||
*
|
||||
* @return the exclusive end position
|
||||
*/
|
||||
public int getExclusiveEnd() {
|
||||
return fOffset + fLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of this <code>TextRange</code>.
|
||||
*
|
||||
* @return a copy of this <code>TextRange</code>
|
||||
*/
|
||||
public TextRange copy() {
|
||||
if (isUndefined())
|
||||
return this;
|
||||
return new TextRange(fOffset, fLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if this text range is the <code>UNDEFINED</code>
|
||||
* text range. Otherwise <code>false</code> is returned.
|
||||
*/
|
||||
public boolean isUndefined() {
|
||||
return UNDEFINED == this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this <code>TextRange</code> is valid. For valid text range the following
|
||||
* expression evaluates to <code>true</code>:
|
||||
* <pre>
|
||||
* getOffset() >= 0 && getLength() >= 0
|
||||
* </pre>
|
||||
*
|
||||
* @return <code>true</code> if this text range is a valid range. Otherwise <code>
|
||||
* false</code>
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return fOffset >= 0 && fLength >= 0;
|
||||
}
|
||||
|
||||
/* package */ boolean isInsertionPoint() {
|
||||
return fLength == 0;
|
||||
}
|
||||
|
||||
/* package */ boolean equals(TextRange range) {
|
||||
return fOffset == range.fOffset && fLength == range.fLength;
|
||||
}
|
||||
|
||||
/* package */ boolean isEqualInsertionPoint(TextRange range) {
|
||||
return fLength == 0 && range.fLength == 0 && fOffset == range.fOffset;
|
||||
}
|
||||
|
||||
/* package */ boolean liesBehind(TextRange range) {
|
||||
return fOffset >= range.fOffset + range.fLength;
|
||||
}
|
||||
|
||||
/* package */ boolean isInsertionPointAt(int o) {
|
||||
return fOffset == o && fLength == 0;
|
||||
}
|
||||
|
||||
/* package */ boolean covers(TextRange other) {
|
||||
if (fLength == 0) { // an insertion point can't cover anything
|
||||
return false;
|
||||
} else if (other.fLength == 0) {
|
||||
int otherOffset= other.fOffset;
|
||||
return fOffset < otherOffset && otherOffset < fOffset + fLength;
|
||||
} else {
|
||||
int otherOffset= other.fOffset;
|
||||
return fOffset <= otherOffset && otherOffset + other.fLength <= fOffset + fLength;
|
||||
}
|
||||
}
|
||||
/* non Java-doc
|
||||
* @see Object#toString()
|
||||
*/
|
||||
public String toString() {
|
||||
StringBuffer buffer= new StringBuffer();
|
||||
buffer.append(TextManipulationMessages.getString("TextRange.offset")); //$NON-NLS-1$
|
||||
buffer.append(fOffset);
|
||||
buffer.append(TextManipulationMessages.getString("TextRange.length")); //$NON-NLS-1$
|
||||
buffer.append(fLength);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (! (obj instanceof TextRange))
|
||||
return false;
|
||||
TextRange other= (TextRange)obj;
|
||||
return fOffset == other.getOffset() && fLength == other.getLength();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return fOffset ^ fLength;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2005 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
* QNX Software System
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.corext.textmanipulation;
|
||||
|
||||
/**
|
||||
* A text region describes a certain range in an <code>ITextBuffer</code>. A region is defined by
|
||||
* its offset into the text buffer and its length.
|
||||
* <p>
|
||||
* A region is considered a value object. Its offset or length do not change over time. </p>
|
||||
* <p>
|
||||
* <bf>NOTE:<bf> This class/interface is part of an interim API that is still under development
|
||||
* and expected to change significantly before reaching stability. It is being made available at
|
||||
* this early stage to solicit feedback from pioneering adopters on the understanding that any
|
||||
* code that uses this API will almost certainly be broken (repeatedly) as the API evolves.</p>
|
||||
*/
|
||||
|
||||
// This class avoids contamination of clients with wrong imports.
|
||||
|
||||
public abstract class TextRegion {
|
||||
|
||||
/**
|
||||
* Returns the offset of the region.
|
||||
*
|
||||
* @return the offset of the region
|
||||
*/
|
||||
public abstract int getOffset();
|
||||
/**
|
||||
* Returns the length of the region.
|
||||
*
|
||||
* @return the length of the region
|
||||
*/
|
||||
public abstract int getLength();
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue