Search in sources :

Example 1 with ActionLabelData

use of com.intellij.psi.codeStyle.EditorNotificationInfo.ActionLabelData in project intellij-community by JetBrains.

the class DetectableIndentOptionsProvider method getNotificationInfo.

@Nullable
@Override
public EditorNotificationInfo getNotificationInfo(@NotNull final Project project, @NotNull final VirtualFile file, @NotNull final FileEditor fileEditor, @NotNull IndentOptions userOptions, @NotNull IndentOptions detectedOptions) {
    final NotificationLabels labels = getNotificationLabels(userOptions, detectedOptions);
    final Editor editor = fileEditor instanceof TextEditor ? ((TextEditor) fileEditor).getEditor() : null;
    if (labels == null || editor == null)
        return null;
    ActionLabelData okAction = new ActionLabelData(ApplicationBundle.message("code.style.indents.detector.accept"), () -> setAccepted(file));
    ActionLabelData disableForSingleFile = new ActionLabelData(labels.revertToOldSettingsLabel, () -> {
        disableForFile(file);
        if (editor instanceof EditorEx) {
            ((EditorEx) editor).reinitSettings();
        }
    });
    ActionLabelData showSettings = new ActionLabelData(ApplicationBundle.message("code.style.indents.detector.show.settings"), () -> ShowSettingsUtilImpl.showSettingsDialog(project, "preferences.sourceCode", "detect indent"));
    final List<ActionLabelData> actions = ContainerUtil.newArrayList(okAction, disableForSingleFile, showSettings);
    return new EditorNotificationInfo() {

        @NotNull
        @Override
        public List<ActionLabelData> getLabelAndActions() {
            return actions;
        }

        @NotNull
        @Override
        public String getTitle() {
            return labels.title;
        }
    };
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) EditorEx(com.intellij.openapi.editor.ex.EditorEx) ActionLabelData(com.intellij.psi.codeStyle.EditorNotificationInfo.ActionLabelData) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ActionLabelData

use of com.intellij.psi.codeStyle.EditorNotificationInfo.ActionLabelData in project intellij-community by JetBrains.

the class DetectedIndentOptionsNotificationProvider method createNotificationPanel.

@Nullable
@Override
public EditorNotificationPanel createNotificationPanel(@NotNull final VirtualFile file, @NotNull FileEditor fileEditor) {
    Boolean notifiedFlag = fileEditor.getUserData(NOTIFIED_FLAG);
    if (fileEditor instanceof TextEditor && notifiedFlag != null) {
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        final Project project = editor.getProject();
        if (project != null) {
            Document document = editor.getDocument();
            PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
            PsiFile psiFile = documentManager.getPsiFile(document);
            final Ref<FileIndentOptionsProvider> indentOptionsProviderRef = new Ref<>();
            if (psiFile != null) {
                CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
                CommonCodeStyleSettings.IndentOptions userOptions = settings.getIndentOptions(psiFile.getFileType());
                CommonCodeStyleSettings.IndentOptions detectedOptions = CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(psiFile, null, false, provider -> {
                    indentOptionsProviderRef.set(provider);
                    return false;
                });
                final FileIndentOptionsProvider provider = indentOptionsProviderRef.get();
                EditorNotificationInfo info = provider != null && !provider.isAcceptedWithoutWarning(project, file) && !userOptions.equals(detectedOptions) ? provider.getNotificationInfo(project, file, fileEditor, userOptions, detectedOptions) : null;
                if (info != null) {
                    EditorNotificationPanel panel = new EditorNotificationPanel().text(info.getTitle());
                    if (info.getIcon() != null) {
                        panel.icon(info.getIcon());
                    }
                    for (final ActionLabelData actionLabelData : info.getLabelAndActions()) {
                        Runnable onClickAction = () -> {
                            actionLabelData.action.run();
                            EditorNotifications.getInstance(project).updateAllNotifications();
                        };
                        panel.createActionLabel(actionLabelData.label, onClickAction);
                    }
                    if (ApplicationManager.getApplication().isUnitTestMode()) {
                        file.putUserData(DETECT_INDENT_NOTIFICATION_SHOWN_KEY, Boolean.TRUE);
                    }
                    return panel;
                }
            }
        }
    }
    return null;
}
Also used : ActionLabelData(com.intellij.psi.codeStyle.EditorNotificationInfo.ActionLabelData) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) TextEditor(com.intellij.openapi.fileEditor.TextEditor) EditorNotificationPanel(com.intellij.ui.EditorNotificationPanel) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Editor (com.intellij.openapi.editor.Editor)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 ActionLabelData (com.intellij.psi.codeStyle.EditorNotificationInfo.ActionLabelData)2 Nullable (org.jetbrains.annotations.Nullable)2 Document (com.intellij.openapi.editor.Document)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 Project (com.intellij.openapi.project.Project)1 Ref (com.intellij.openapi.util.Ref)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiFile (com.intellij.psi.PsiFile)1 EditorNotificationPanel (com.intellij.ui.EditorNotificationPanel)1