summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/ide/scratch/CreateScratchFileAction.java
blob: 7a190f6be71c35b7123d6b49c8c746b50cf131ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide.scratch;

import com.intellij.lang.DependentLanguage;
import com.intellij.lang.InjectableLanguage;
import com.intellij.lang.Language;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.ContainerUtilRt;
import com.intellij.util.ui.EmptyIcon;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * @author ignatov
 */
public class CreateScratchFileAction extends AnAction implements DumbAware {
  public static final int MAX_VISIBLE_SIZE = 20;

  public CreateScratchFileAction() {
    super("Create Scratch File...", "New Scratch File", null);
  }

  @Override
  public void update(AnActionEvent e) {
    e.getPresentation().setEnabledAndVisible(e.getProject() != null && Registry.is("ide.scratch.enabled"));
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null) return;
    List<Language> languages = getLanguages();
    if (languages.isEmpty()) return;
    BaseListPopupStep<Language> step =
      new BaseListPopupStep<Language>("Specify the language", languages) {
        @NotNull
        @Override
        public String getTextFor(Language value) {
          return value.getDisplayName();
        }

        @Override
        public String getIndexedString(Language value) {
          return value.getDisplayName();
        }

        @Override
        public boolean isSpeedSearchEnabled() {
          return true;
        }

        @Override
        public PopupStep onChosen(Language selectedValue, boolean finalChoice) {
          doAction(project, selectedValue);
          return null;
        }

        @Override
        public Icon getIconFor(Language language) {
          LanguageFileType associatedLanguage = language.getAssociatedFileType();
          return associatedLanguage != null ? associatedLanguage.getIcon() : null;
        }
      };

    Language previous = ScratchpadManager.getInstance(project).getLatestLanguage();
    final String previousName = previous != null ? previous.getDisplayName() : "Plain text";

    if (previousName != null) {
      int defaultOption = ContainerUtil.indexOf(languages, new Condition<Language>() {
        @Override
        public boolean value(Language module) {
          return module.getDisplayName().equals(previousName);
        }
      });
      if (defaultOption >= 0) {
        step.setDefaultOptionIndex(defaultOption);
      }
    }

    ListPopup popup = updatePopupSize(JBPopupFactory.getInstance().createListPopup(step), languages);
    popup.showCenteredInCurrentWindow(project);
  }

  @NotNull
  public static ListPopup updatePopupSize(@NotNull ListPopup popup, @NotNull List<Language> languages) {
    int nameLen = 0;
    for (Language language : languages) {
      nameLen = Math.max(nameLen, language.getDisplayName().length());
    }
    if (languages.size() > MAX_VISIBLE_SIZE) {
      Dimension size = new JLabel(StringUtil.repeatSymbol('a', nameLen), EmptyIcon.ICON_16, SwingConstants.LEFT).getMinimumSize();
      size.height *= MAX_VISIBLE_SIZE;
      popup.setSize(size);
    }
    return popup;
  }

  public static void doAction(@NotNull Project project, @NotNull Language language) {
    VirtualFile file = ScratchpadManager.getInstance(project).createScratchFile(language);
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file);
    FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
  }

  @NotNull
  public static List<Language> getLanguages() {
    Set<Language> result = ContainerUtilRt.newTreeSet(new Comparator<Language>() {
      @Override
      public int compare(@NotNull Language l1, @NotNull Language l2) {
        return l1.getDisplayName().compareTo(l2.getDisplayName());
      }
    });
    for (Language lang : Language.getRegisteredLanguages()) {
      if (!StringUtil.isEmpty(lang.getDisplayName())) result.add(lang);
      for (Language dialect : lang.getDialects()) result.add(dialect);
    }
    return ContainerUtil.filter(result, new Condition<Language>() {
      @Override
      public boolean value(Language lang) {
        if (lang instanceof DependentLanguage || lang instanceof InjectableLanguage) return false;
        LanguageFileType type = lang.getAssociatedFileType();
        if (type == null) return false;
        String name = lang.getDisplayName();
        if (StringUtil.startsWith(name, "<") || StringUtil.startsWith(name, "[") || StringUtil.isEmpty(name)) return false;
        return !StringUtil.isEmpty(type.getDefaultExtension());
      }
    });
  }
}