Skip to content

Commit

Permalink
Simplify Status creation
Browse files Browse the repository at this point in the history
Ensures that correct plugin ids are used as right now claiming issue
comes from LSP4E bundle in some places is misleading.
  • Loading branch information
akurtakov committed Jun 28, 2022
1 parent 6ac4c8a commit 7c3ec83
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public static CorrosionPlugin getDefault() {
* @param status status to log
*/
public static void logError(String message) {
getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, INTERNAL_ERROR, message, null));
getDefault().getLog().log(Status.error(message));
}

public static void logError(Throwable t) {
logError(new Status(IStatus.ERROR, PLUGIN_ID, t.getMessage(), t));
logError(Status.error(t.getMessage(), t));
}

public static void logError(IStatus s) {
Expand Down
13 changes: 4 additions & 9 deletions org.eclipse.corrosion/src/org/eclipse/corrosion/RustManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2017, 2021 Red Hat Inc. and others.
* Copyright (c) 2017, 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -278,9 +278,7 @@ public static boolean setSystemProperties() {
return true;
}
}
CorrosionPlugin.getDefault().getLog()
.log(new Status(IStatus.ERROR, CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
Messages.RLSStreamConnectionProvider_unableToSet));
CorrosionPlugin.getDefault().getLog().log(Status.error(Messages.RLSStreamConnectionProvider_unableToSet));
return false;
}

Expand All @@ -291,8 +289,7 @@ public static File getLanguageServerConfiguration() {
.getString(CorrosionPreferenceInitializer.RLS_CONFIGURATION_PATH_PREFERENCE);
if (preferencePath.isEmpty()) {
CorrosionPlugin.getDefault().getLog()
.log(new Status(IStatus.WARNING, CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
Messages.RLSStreamConnectionProvider_rlsConfigurationNotSet));
.log(Status.warning(Messages.RLSStreamConnectionProvider_rlsConfigurationNotSet));
return null;
}
return new File(preferencePath);
Expand All @@ -303,9 +300,7 @@ public static File getLanguageServerExecutable() {
IPreferenceStore preferenceStore = plugin.getPreferenceStore();
String rlsPath = preferenceStore.getString(CorrosionPreferenceInitializer.RLS_PATH_PREFERENCE);
if (rlsPath.isEmpty()) {
CorrosionPlugin.getDefault().getLog()
.log(new Status(IStatus.ERROR, CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
Messages.RLSStreamConnectionProvider_rlsNotFound));
CorrosionPlugin.getDefault().getLog().log(Status.error(Messages.RLSStreamConnectionProvider_rlsNotFound));
return null;
}
return new File(rlsPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2017, 2018 Red Hat Inc. and others.
* Copyright (c) 2017, 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -24,7 +24,6 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.corrosion.CorrosionPlugin;
import org.eclipse.corrosion.CorrosionPreferencePage;
Expand Down Expand Up @@ -108,15 +107,11 @@ public Object getInitializationOptions(URI rootUri) {
try (JsonReader reader = new JsonReader(new FileReader(settingsFile))) {
return gson.fromJson(reader, HashMap.class);
} catch (FileNotFoundException e) {
CorrosionPlugin.getDefault().getLog()
.log(new Status(IStatus.INFO, CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
MessageFormat.format(Messages.RLSStreamConnectionProvider_rlsConfigurationNotFound,
settingsFile)));
CorrosionPlugin.getDefault().getLog().log(Status.info(MessageFormat
.format(Messages.RLSStreamConnectionProvider_rlsConfigurationNotFound, settingsFile)));
} catch (Throwable e) {
CorrosionPlugin.getDefault().getLog()
.log(new Status(IStatus.ERROR, CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
MessageFormat.format(Messages.RLSStreamConnectionProvider_rlsConfigurationError,
settingsFile, e)));
CorrosionPlugin.getDefault().getLog().log(Status.error(MessageFormat
.format(Messages.RLSStreamConnectionProvider_rlsConfigurationError, settingsFile, e)));
}
}
return getDefaultInitializationOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2017 Red Hat Inc. and others.
* Copyright (c) 2017, 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -63,7 +63,8 @@ public ImplementationsSearchQuery(int offset, LSPDocumentInfo info) throws BadLo
this.filename = resource != null ? resource.getName() : info.getFileUri().toString();
}

@Override public IStatus run(IProgressMonitor monitor) {
@Override
public IStatus run(IProgressMonitor monitor) {
startTime = System.currentTimeMillis();
// Cancel last references future if needed.
if (references != null) {
Expand All @@ -78,24 +79,25 @@ public ImplementationsSearchQuery(int offset, LSPDocumentInfo info) throws BadLo
params.setContext(new ReferenceContext(true));
params.setTextDocument(new TextDocumentIdentifier(info.getFileUri().toString()));
params.setPosition(position);
info.getInitializedLanguageClient().thenCompose(languageServer -> ((RLSServerInterface) languageServer).implementations(params)).thenAccept(locs -> {
// Loop for each LSP Location and convert it to Match search.
for (Location loc : locs) {
Match match = toMatch(loc);
result.addMatch(match);
}
});
info.getInitializedLanguageClient()
.thenCompose(languageServer -> ((RLSServerInterface) languageServer).implementations(params))
.thenAccept(locs -> {
// Loop for each LSP Location and convert it to Match search.
for (Location loc : locs) {
Match match = toMatch(loc);
result.addMatch(match);
}
});
return Status.OK_STATUS;
} catch (Exception ex) {
return new Status(IStatus.ERROR, LanguageServerPlugin.getDefault().getBundle().getSymbolicName(), ex.getMessage(), ex);
return Status.error(ex.getMessage(), ex);
}
}

/**
* Convert the given LSP {@link Location} to Eclipse search {@link Match}.
*
* @param location
* the LSP location to convert.
* @param location the LSP location to convert.
* @return the converted Eclipse search {@link Match}.
*/
private static Match toMatch(Location location) {
Expand All @@ -107,42 +109,51 @@ private static Match toMatch(Location location) {
int endOffset = LSPEclipseUtils.toOffset(location.getRange().getEnd(), document);

IRegion lineInformation = document.getLineInformationOfOffset(startOffset);
LineElement lineEntry = new LineElement(resource, document.getLineOfOffset(startOffset), lineInformation.getOffset(), document.get(lineInformation.getOffset(), lineInformation.getLength()));
LineElement lineEntry = new LineElement(resource, document.getLineOfOffset(startOffset),
lineInformation.getOffset(),
document.get(lineInformation.getOffset(), lineInformation.getLength()));
return new FileMatch((IFile) resource, startOffset, endOffset - startOffset, lineEntry);

}
Position startPosition = location.getRange().getStart();
LineElement lineEntry = new LineElement(resource, startPosition.getLine(), 0, String.format("%s:%s", startPosition.getLine(), startPosition.getCharacter())); //$NON-NLS-1$
LineElement lineEntry = new LineElement(resource, startPosition.getLine(), 0,
String.format("%s:%s", startPosition.getLine(), startPosition.getCharacter())); //$NON-NLS-1$
return new FileMatch((IFile) resource, 0, 0, lineEntry);
} catch (BadLocationException ex) {
LanguageServerPlugin.logError(ex);
}
return null;
}

@Override public ISearchResult getSearchResult() {
@Override
public ISearchResult getSearchResult() {
if (result == null) {
result = new FileSearchResult(this);
}
return result;
}

@Override public String getLabel() {
@Override
public String getLabel() {
return Messages.ImplementationsSearchQuery_implementations;
}

@Override public String getResultLabel(int nMatches) {
@Override
public String getResultLabel(int nMatches) {
long time = 0;
if (startTime > 0) {
time = System.currentTimeMillis() - startTime;
}
if (nMatches == 1) {
return NLS.bind(Messages.ImplementationsSearchQuery_oneReference, new Object[] { filename, position.getLine() + 1, position.getCharacter() + 1, time });
return NLS.bind(Messages.ImplementationsSearchQuery_oneReference,
new Object[] { filename, position.getLine() + 1, position.getCharacter() + 1, time });
}
return NLS.bind(Messages.ImplementationsSearchQuery_severalReferences, new Object[] { filename, position.getLine() + 1, position.getCharacter() + 1, nMatches, time });
return NLS.bind(Messages.ImplementationsSearchQuery_severalReferences,
new Object[] { filename, position.getLine() + 1, position.getCharacter() + 1, nMatches, time });
}

@Override public boolean isFileNameSearch() {
@Override
public boolean isFileNameSearch() {
// Return false to display lines where references are found
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2019, 2021 Fraunhofer FOKUS and others.
* Copyright (c) 2019, 2022 Fraunhofer FOKUS and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -32,7 +32,6 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.corrosion.CorrosionPlugin;
Expand Down Expand Up @@ -77,7 +76,7 @@ public Object execute(ExecutionEvent event, Command command, IPath context) thro
// RLS sent unknown command format, we cannot give advice to user, just log
// the error
String msg = NLS.bind(Messages.LaunchHandler_unableToLaunchCommand, command);
CorrosionPlugin.logError(new Status(IStatus.ERROR, CorrosionPlugin.PLUGIN_ID, msg));
CorrosionPlugin.logError(Status.error(msg));
}

IProject project = ResourcesPlugin.getWorkspace().getRoot().getFile(context).getProject();
Expand All @@ -90,7 +89,7 @@ public Object execute(ExecutionEvent event, Command command, IPath context) thro
// If command is not present, we have RLS command we don't know how to execute.
// We cannot give user advice on what to do, just log the error
String msg = NLS.bind(Messages.LaunchHandler_unableToLaunchCommand, command);
CorrosionPlugin.logError(new Status(IStatus.ERROR, CorrosionPlugin.PLUGIN_ID, msg));
CorrosionPlugin.logError(Status.error(msg));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2020, 2021 Red Hat Inc. and others.
* Copyright (c) 2020, 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -36,7 +36,6 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.corrosion.CorrosionPlugin;
Expand All @@ -60,19 +59,15 @@ public class CargoSourceUtils {
private static final String ATTR_MEMENTO = "memento"; //$NON-NLS-1$

public static String getCommonSourceLocationsMemento(ICargoSourceLocation[] locations) {
Document document = null;
Throwable ex = null;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element element = document.createElement(NAME_COMMON_SOURCE_LOCATIONS);
document.appendChild(element);
saveSourceLocations(document, element, locations);
return serializeDocument(document, true);
} catch (ParserConfigurationException | TransformerException e) {
ex = e;
CorrosionPlugin.logError(Status.error("Error saving common source settings.", e)); //$NON-NLS-1$
}
CorrosionPlugin.logError(
new Status(IStatus.ERROR, CorrosionPlugin.PLUGIN_ID, 0, "Error saving common source settings.", ex)); //$NON-NLS-1$
return null;
}

Expand Down Expand Up @@ -100,15 +95,8 @@ public static ICargoSourceLocation[] getCommonSourceLocationsFromMemento(String
Element root = parser.parse(source).getDocumentElement();
if (root.getNodeName().equalsIgnoreCase(NAME_COMMON_SOURCE_LOCATIONS))
result = initializeSourceLocations(root);
} catch (ParserConfigurationException e) {
CorrosionPlugin.logError(new Status(IStatus.ERROR, CorrosionPlugin.PLUGIN_ID, 0,
"Error initializing common source settings.", e)); //$NON-NLS-1$
} catch (SAXException e) {
CorrosionPlugin.logError(new Status(IStatus.ERROR, CorrosionPlugin.PLUGIN_ID, 0,
"Error initializing common source settings.", e)); //$NON-NLS-1$
} catch (IOException e) {
CorrosionPlugin.logError(new Status(IStatus.ERROR, CorrosionPlugin.PLUGIN_ID, 0,
"Error initializing common source settings.", e)); //$NON-NLS-1$
} catch (ParserConfigurationException | SAXException | IOException e) {
CorrosionPlugin.logError(Status.error("Error initializing common source settings.", e)); //$NON-NLS-1$
}
}
return result;
Expand Down

0 comments on commit 7c3ec83

Please sign in to comment.