1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 14:12:10 +02:00

Code cleanup.

Change-Id: Ibe8f9fc65ddfc3dafe187935bd7f605be8ea1820
This commit is contained in:
Sergey Prigogin 2016-05-11 15:50:06 -07:00
parent 2f58cf231e
commit decaa7f934

View file

@ -45,12 +45,11 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable; import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.action.Action; import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadLocationException;
@ -107,7 +106,6 @@ import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
import org.eclipse.cdt.internal.ui.text.LineComparator; import org.eclipse.cdt.internal.ui.text.LineComparator;
public class EditorUtility { public class EditorUtility {
/** /**
* The ID of the default text editor * The ID of the default text editor
*/ */
@ -140,8 +138,8 @@ public class EditorUtility {
} }
/** /**
* Opens an editor for an element such as <code>ICElement</code>, * Opens an editor for an element such as {@code ICElement},
* <code>IFile</code>, or <code>IStorage</code>. * {@code IFile}, or {@code IStorage}.
* The editor is activated by default. * The editor is activated by default.
* @return the IEditorPart or null if wrong element type or opening failed * @return the IEditorPart or null if wrong element type or opening failed
*/ */
@ -327,7 +325,7 @@ public class EditorUtility {
* the editor input. * the editor input.
* *
* @param location a file system location * @param location a file system location
* @param element an element related to the target file, may be <code>null</code> * @param element an element related to the target file, may be {@code null}
* @throws PartInitException * @throws PartInitException
*/ */
public static IEditorPart openInEditor(IPath location, ICElement element) throws PartInitException { public static IEditorPart openInEditor(IPath location, ICElement element) throws PartInitException {
@ -346,14 +344,14 @@ public class EditorUtility {
/** /**
* Utility method to get an editor input for the given file system location. * Utility method to get an editor input for the given file system location.
* If the location denotes a workspace file, a <code>FileEditorInput</code> * If the location denotes a workspace file, a {@code FileEditorInput}
* is returned, otherwise, the input is an <code>IURIEditorInput</code> * is returned, otherwise, the input is an {@code IURIEditorInput}
* assuming the location points to an existing file in the file system. * assuming the location points to an existing file in the file system.
* The <code>ICElement</code> is used to determine the associated project * The {@code ICElement} is used to determine the associated project
* in case the location can not be resolved to a workspace <code>IFile</code>. * in case the location can not be resolved to a workspace {@code IFile}.
* *
* @param locationURI a valid file system location * @param locationURI a valid file system location
* @param context an element related to the target file, may be <code>null</code> * @param context an element related to the target file, may be {@code null}
* @return an editor input * @return an editor input
*/ */
public static IEditorInput getEditorInputForLocation(URI locationURI, ICElement context) { public static IEditorInput getEditorInputForLocation(URI locationURI, ICElement context) {
@ -472,8 +470,8 @@ public class EditorUtility {
* a resource with the same project of the context element are preferred. * a resource with the same project of the context element are preferred.
* *
* @param location a valid file system location * @param location a valid file system location
* @param context an element related to the target file, may be <code>null</code> * @param context an element related to the target file, may be {@code null}
* @return an <code>IFile</code> or <code>null</code> * @return an {@code IFile} or {@code null}
*/ */
public static IFile getWorkspaceFileAtLocation(IPath location, ICElement context) { public static IFile getWorkspaceFileAtLocation(IPath location, ICElement context) {
IProject project= null; IProject project= null;
@ -509,8 +507,8 @@ public class EditorUtility {
* a resource with the same project of the context element are preferred. * a resource with the same project of the context element are preferred.
* *
* @param locationURI a valid Eclipse file system URI * @param locationURI a valid Eclipse file system URI
* @param context an element related to the target file, may be <code>null</code> * @param context an element related to the target file, may be {@code null}
* @return an <code>IFile</code> or <code>null</code> * @return an {@code IFile} or {@code null}
*/ */
public static IFile getWorkspaceFileAtLocation(URI locationURI, ICElement context) { public static IFile getWorkspaceFileAtLocation(URI locationURI, ICElement context) {
IProject project= null; IProject project= null;
@ -571,7 +569,7 @@ public class EditorUtility {
* the workspace-wide content-type definitions. * the workspace-wide content-type definitions.
* *
* @param name the file name * @param name the file name
* @return a valid editor id, never <code>null</code> * @return a valid editor id, never {@code null}
*/ */
public static String getEditorID(String name) { public static String getEditorID(String name) {
try { try {
@ -593,8 +591,8 @@ public class EditorUtility {
* the workspace-wide content-type definitions. * the workspace-wide content-type definitions.
* *
* @param input the editor input * @param input the editor input
* @param inputObject the input object (used to create the editor input) or <code>null</code> * @param inputObject the input object (used to create the editor input) or {@code null}
* @return a valid editor id, never <code>null</code> * @return a valid editor id, never {@code null}
*/ */
public static String getEditorID(IEditorInput input, Object inputObject) { public static String getEditorID(IEditorInput input, Object inputObject) {
ICElement cElement= null; ICElement cElement= null;
@ -667,7 +665,7 @@ public class EditorUtility {
* Maps the localized modifier name to a code in the same * Maps the localized modifier name to a code in the same
* manner as #findModifier. * manner as #findModifier.
* *
* @return the SWT modifier bit, or <code>0</code> if no match was found * @return the SWT modifier bit, or {@code 0} if no match was found
*/ */
public static int findLocalizedModifier(String token) { public static int findLocalizedModifier(String token) {
if (token == null) if (token == null)
@ -739,7 +737,7 @@ public class EditorUtility {
} }
/** /**
* Returns the C project for a given editor input or <code>null</code> if no corresponding * Returns the C project for a given editor input or {@code null} if no corresponding
* C project exists. * C project exists.
* *
* @param input the editor input * @param input the editor input
@ -773,15 +771,15 @@ public class EditorUtility {
/** /**
* Returns an array of all editors that have an unsaved content. If the identical content is * Returns an array of all editors that have an unsaved content. If the identical content is
* presented in more than one editor, only one of those editor parts is part of the result. * presented in more than one editor, only one of those editor parts is part of the result.
* @param skipNonResourceEditors if <code>true</code>, editors whose inputs do not adapt to {@link IResource} * @param skipNonResourceEditors if {@code true}, editors whose inputs do not adapt to {@link IResource}
* are not saved * are not saved
* *
* @return an array of dirty editor parts * @return an array of dirty editor parts
* @since 5.3 * @since 5.3
*/ */
public static IEditorPart[] getDirtyEditors(boolean skipNonResourceEditors) { public static IEditorPart[] getDirtyEditors(boolean skipNonResourceEditors) {
Set<IEditorInput> inputs= new HashSet<IEditorInput>(); Set<IEditorInput> inputs= new HashSet<>();
List<IEditorPart> result= new ArrayList<IEditorPart>(0); List<IEditorPart> result= new ArrayList<>();
IWorkbench workbench= PlatformUI.getWorkbench(); IWorkbench workbench= PlatformUI.getWorkbench();
IWorkbenchWindow[] windows= workbench.getWorkbenchWindows(); IWorkbenchWindow[] windows= workbench.getWorkbenchWindows();
for (IWorkbenchWindow window : windows) { for (IWorkbenchWindow window : windows) {
@ -819,13 +817,13 @@ public class EditorUtility {
* Returns the editors to save before performing global C-related * Returns the editors to save before performing global C-related
* operations. * operations.
* *
* @param saveUnknownEditors <code>true</code> iff editors with unknown buffer management should also be saved * @param saveUnknownEditors {@code true} iff editors with unknown buffer management should also be saved
* @return the editors to save * @return the editors to save
* @since 5.3 * @since 5.3
*/ */
public static IEditorPart[] getDirtyEditorsToSave(boolean saveUnknownEditors) { public static IEditorPart[] getDirtyEditorsToSave(boolean saveUnknownEditors) {
Set<IEditorInput> inputs= new HashSet<IEditorInput>(); Set<IEditorInput> inputs= new HashSet<>();
List<IEditorPart> result= new ArrayList<IEditorPart>(0); List<IEditorPart> result= new ArrayList<>();
IWorkbench workbench= PlatformUI.getWorkbench(); IWorkbench workbench= PlatformUI.getWorkbench();
IWorkbenchWindow[] windows= workbench.getWorkbenchWindows(); IWorkbenchWindow[] windows= workbench.getWorkbenchWindows();
for (IWorkbenchWindow window : windows) { for (IWorkbenchWindow window : windows) {
@ -854,7 +852,7 @@ public class EditorUtility {
* (Leaving them dirty would cause problems, since the file buffer could have been * (Leaving them dirty would cause problems, since the file buffer could have been
* modified but the C model is not reconciled.) * modified but the C model is not reconciled.)
* *
* If <code>saveUnknownEditors</code> is <code>true</code>, save all editors * If {@code saveUnknownEditors} is {@code true}, save all editors
* whose implementation is probably not based on file buffers. * whose implementation is probably not based on file buffers.
*/ */
IResource resource= input.getAdapter(IResource.class); IResource resource= input.getAdapter(IResource.class);
@ -899,9 +897,6 @@ public class EditorUtility {
try { try {
SafeRunner.run(new ISafeRunnable() { SafeRunner.run(new ISafeRunnable() {
/*
* @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
*/
@Override @Override
public void handleException(Throwable exception) { public void handleException(Throwable exception) {
CUIPlugin.log(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, CUIPlugin.log(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID,
@ -913,16 +908,14 @@ public class EditorUtility {
result[0]= null; result[0]= null;
} }
/*
* @see org.eclipse.core.runtime.ISafeRunnable#run()
*/
@Override @Override
public void run() throws Exception { public void run() throws Exception {
monitor.beginTask(Messages.EditorUtility_calculatingChangedRegions_message, 20); SubMonitor progress =
SubMonitor.convert(monitor, Messages.EditorUtility_calculatingChangedRegions_message, 4);
IFileStore fileStore= buffer.getFileStore(); IFileStore fileStore= buffer.getFileStore();
ITextFileBufferManager fileBufferManager= FileBuffers.createTextFileBufferManager(); ITextFileBufferManager fileBufferManager= FileBuffers.createTextFileBufferManager();
fileBufferManager.connectFileStore(fileStore, getSubProgressMonitor(monitor, 15)); fileBufferManager.connectFileStore(fileStore, progress.split(3));
try { try {
IDocument currentDocument= buffer.getDocument(); IDocument currentDocument= buffer.getDocument();
IDocument oldDocument= IDocument oldDocument=
@ -930,14 +923,13 @@ public class EditorUtility {
result[0]= getChangedLineRegions(oldDocument, currentDocument); result[0]= getChangedLineRegions(oldDocument, currentDocument);
} finally { } finally {
fileBufferManager.disconnectFileStore(fileStore, getSubProgressMonitor(monitor, 5)); fileBufferManager.disconnectFileStore(fileStore, progress.split(1));
monitor.done();
} }
} }
/** /**
* Return regions of all lines which differ comparing <code>oldDocument</code>s content * Return regions of all lines which differ comparing {@code oldDocument}s content
* with <code>currentDocument</code>s content. Successive lines are merged into one region. * with {@code currentDocument}s content. Successive lines are merged into one region.
* *
* @param oldDocument a document containing the old content * @param oldDocument a document containing the old content
* @param currentDocument a document containing the current content * @param currentDocument a document containing the current content
@ -961,7 +953,7 @@ public class EditorUtility {
// 2. Successive changed lines are merged into on RangeDifference // 2. Successive changed lines are merged into on RangeDifference
// forAll r1,r2 element differences: r1.rightStart() < r2.rightStart() -> r1.rightEnd() < r2.rightStart // forAll r1,r2 element differences: r1.rightStart() < r2.rightStart() -> r1.rightEnd() < r2.rightStart
List<IRegion> regions= new ArrayList<IRegion>(); List<IRegion> regions= new ArrayList<>();
final int numberOfLines= currentDocument.getNumberOfLines(); final int numberOfLines= currentDocument.getNumberOfLines();
for (RangeDifference curr : differences) { for (RangeDifference curr : differences) {
if (curr.kind() == RangeDifference.CHANGE) { if (curr.kind() == RangeDifference.CHANGE) {
@ -1001,26 +993,10 @@ public class EditorUtility {
return result[0]; return result[0];
} }
/**
* Creates and returns a new sub-progress monitor for
* the given parent monitor.
*
* @param monitor the parent progress monitor
* @param ticks the number of work ticks allocated from the parent monitor
* @return the new sub-progress monitor
*/
private static IProgressMonitor getSubProgressMonitor(IProgressMonitor monitor, int ticks) {
if (monitor != null)
return new SubProgressMonitor(monitor, ticks, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
return new NullProgressMonitor();
}
/** /**
* Returns the project contains the resource, which is currently open in the active editor. * Returns the project contains the resource, which is currently open in the active editor.
* If the active part is no ITextEditor or if the editorInput is no FileEditorInput, * If the active part is no ITextEditor or if the editorInput is no FileEditorInput,
* <code>null</code> is returned. * {@code null} is returned.
* *
* @return the project which the selected editor input belongs to or null * @return the project which the selected editor input belongs to or null
*/ */
@ -1050,6 +1026,6 @@ public class EditorUtility {
* @param editor The editor to be converted or null if there is nothing to convert. * @param editor The editor to be converted or null if there is nothing to convert.
*/ */
public static ITextEditor getTextEditor(IEditorPart editor) { public static ITextEditor getTextEditor(IEditorPart editor) {
return editor == null ? null : (ITextEditor) editor.getAdapter(ITextEditor.class); return editor == null ? null : editor.getAdapter(ITextEditor.class);
} }
} }