Search in sources :

Example 6 with OpenFileHyperlink

use of com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink in project android by JetBrains.

the class GradleDslMethodNotFoundErrorHandler method getQuickFixHyperlinks.

private static void getQuickFixHyperlinks(@NotNull NotificationData notification, @NotNull Project project, @NotNull String text) {
    List<NotificationHyperlink> hyperlinks = new ArrayList<>();
    String filePath = notification.getFilePath();
    VirtualFile file = filePath != null ? LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath) : null;
    if (file != null && FN_BUILD_GRADLE.equals(file.getName())) {
        updateNotificationWithBuildFile(project, file, notification, text);
        return;
    }
    if (file != null && notification.getLine() > 0 && notification.getNavigatable() == null) {
        OpenFileHyperlink hyperlink = new OpenFileHyperlink(filePath, notification.getLine() - 1);
        hyperlinks.add(hyperlink);
    }
    SyncMessages.getInstance(project).updateNotification(notification, text, hyperlinks);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) OpenFileHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)

Example 7 with OpenFileHyperlink

use of com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink in project android by JetBrains.

the class MissingDependencyErrorHandler method handleError.

@Override
public boolean handleError(@NotNull ExternalSystemException error, @NotNull NotificationData notification, @NotNull Project project) {
    //noinspection ThrowableResultOfMethodCallIgnored
    Throwable rootCause = getRootCause(error);
    String text = rootCause.getMessage();
    List<String> message = Splitter.on('\n').omitEmptyStrings().trimResults().splitToList(text);
    String firstLine = message.get(0);
    Matcher matcher = MISSING_MATCHING_DEPENDENCY_PATTERN.matcher(firstLine);
    if (matcher.matches()) {
        String dependency = matcher.group(1);
        handleMissingDependency(notification, project, firstLine, dependency, Collections.emptyList());
        return true;
    }
    String lastLine = message.get(message.size() - 1);
    matcher = MISSING_DEPENDENCY_PATTERN.matcher(firstLine);
    if (matcher.matches() && message.size() > 1 && message.get(1).startsWith("Required by:")) {
        String dependency = matcher.group(1);
        List<NotificationHyperlink> hyperlinks = new ArrayList<>();
        if (isNotEmpty(dependency)) {
            if (lastLine != null) {
                Pair<String, Integer> errorLocation = getErrorLocation(lastLine);
                if (errorLocation != null) {
                    // We have a location in file, show the "Open File" hyperlink.
                    String filePath = errorLocation.getFirst();
                    int line = errorLocation.getSecond();
                    hyperlinks.add(new OpenFileHyperlink(filePath, line - 1));
                }
            }
            handleMissingDependency(notification, project, error.getMessage(), dependency, hyperlinks);
            return true;
        }
    }
    for (String line : message) {
        // This happens when Gradle cannot find the Android Gradle plug-in in Maven Central or jcenter.
        if (line == null) {
            continue;
        }
        matcher = MISSING_MATCHING_DEPENDENCY_PATTERN.matcher(line);
        if (matcher.matches()) {
            String dependency = matcher.group(1);
            handleMissingDependency(notification, project, line, dependency, Collections.emptyList());
            return true;
        }
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) OpenFileHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)

Example 8 with OpenFileHyperlink

use of com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink in project android by JetBrains.

the class MissingAndroidSdkErrorHandler method getQuickFixHyperlinks.

@Override
@NotNull
protected List<NotificationHyperlink> getQuickFixHyperlinks(@NotNull NotificationData notification, @NotNull Project project, @NotNull String text) {
    // If we got this far, local.properties exists.
    // Confirmed in findErrorMessage().
    File file = new File(getBaseDirPath(project), FN_LOCAL_PROPERTIES);
    int lineNumber = 0;
    BufferedReader reader = null;
    try {
        //noinspection IOResourceOpenedButNotSafelyClosed
        reader = new BufferedReader(new FileReader(file));
        int counter = 0;
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.startsWith(SDK_DIR_PROPERTY)) {
                lineNumber = counter;
                break;
            }
            counter++;
        }
    } catch (IOException e) {
        Logger.getInstance(getClass()).info("Unable to read file: " + file.getPath(), e);
    } finally {
        try {
            close(reader, true);
        } catch (IOException ignored) {
        // Cannot happen
        }
    }
    List<NotificationHyperlink> hyperlinks = new ArrayList<>();
    hyperlinks.add(new OpenFileHyperlink(file.getPath(), lineNumber));
    return hyperlinks;
}
Also used : BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) OpenFileHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)8 OpenFileHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink)8 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)5 AndroidPluginInfo (com.android.tools.idea.gradle.plugin.AndroidPluginInfo)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 LocalPackage (com.android.repository.api.LocalPackage)1 ProgressIndicator (com.android.repository.api.ProgressIndicator)1 RepositoryPackages (com.android.repository.impl.meta.RepositoryPackages)1 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)1 FixAndroidGradlePluginVersionHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.FixAndroidGradlePluginVersionHyperlink)1 FixBuildToolsVersionHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.FixBuildToolsVersionHyperlink)1 InstallBuildToolsHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.InstallBuildToolsHyperlink)1 SyncMessage (com.android.tools.idea.gradle.project.sync.messages.SyncMessage)1 SyncMessagesStub (com.android.tools.idea.gradle.project.sync.messages.SyncMessagesStub)1 PositionInFile (com.android.tools.idea.gradle.util.PositionInFile)1 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)1 Document (com.intellij.openapi.editor.Document)1 FileUtil.loadFile (com.intellij.openapi.util.io.FileUtil.loadFile)1