mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Save scratch files creation history in workspace.xml Move last used on top
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
package com.intellij.ide.scratch;
|
||||
|
||||
import com.intellij.featureStatistics.FeatureUsageTracker;
|
||||
import com.intellij.ide.util.PropertiesComponent;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.LanguageUtil;
|
||||
import com.intellij.lang.StdLanguages;
|
||||
@@ -36,13 +37,15 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.ui.EmptyIcon;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -59,19 +62,29 @@ public class NewScratchFileAction extends AnAction implements DumbAware {
|
||||
public void update(@NotNull AnActionEvent e) {
|
||||
e.getPresentation().setEnabledAndVisible(e.getProject() != null && Registry.is("ide.scratch.enabled"));
|
||||
}
|
||||
|
||||
public static List<String> getLastUsedLanguagesIds(Project project) {
|
||||
String[] values = PropertiesComponent.getInstance(project).getValues(ScratchpadManager.class.getName());
|
||||
if (values == null) {
|
||||
return ContainerUtil.emptyList();
|
||||
}
|
||||
return ContainerUtil.list(values);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
final Project project = e.getProject();
|
||||
if (project == null) return;
|
||||
Language previous = ScratchpadManager.getInstance(project).getLatestLanguage();
|
||||
if (previous == null) {
|
||||
PsiFile file = e.getData(CommonDataKeys.PSI_FILE);
|
||||
if (file != null) {
|
||||
previous = file.getLanguage();
|
||||
}
|
||||
|
||||
Language context = null;
|
||||
PsiFile file = e.getData(CommonDataKeys.PSI_FILE);
|
||||
|
||||
if (file != null) {
|
||||
context = file.getLanguage();
|
||||
}
|
||||
ListPopup popup = buildLanguagePopup(previous, new Consumer<Language>() {
|
||||
|
||||
ListPopup popup = buildLanguagePopup(project, context, new Consumer<Language>() {
|
||||
@Override
|
||||
public void consume(Language language) {
|
||||
FeatureUsageTracker.getInstance().triggerFeatureUsed("scratch");
|
||||
@@ -83,8 +96,26 @@ public class NewScratchFileAction extends AnAction implements DumbAware {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static ListPopup buildLanguagePopup(@Nullable Language previous, final Consumer<Language> onChoosen) {
|
||||
static ListPopup buildLanguagePopup(@NotNull Project project, @Nullable Language context, final Consumer<Language> onChoosen) {
|
||||
List<Language> languages = LanguageUtil.getFileLanguages();
|
||||
final List<String> ids = new ArrayList<String>(getLastUsedLanguagesIds(project));
|
||||
if (context != null) {
|
||||
ids.add(context.getID());
|
||||
}
|
||||
if (ids.isEmpty()) {
|
||||
ids.add(StdLanguages.TEXT.getID());
|
||||
}
|
||||
|
||||
ContainerUtil.sort(languages, new Comparator<Language>() {
|
||||
@Override
|
||||
public int compare(Language o1, Language o2) {
|
||||
int ind1 = ids.indexOf(o1.getID());
|
||||
int ind2 = ids.indexOf(o2.getID());
|
||||
if (ind1 == -1) ind1 = 666;
|
||||
if (ind2 == -1) ind2 = 666;
|
||||
return ind1 - ind2;
|
||||
}
|
||||
});
|
||||
BaseListPopupStep<Language> step =
|
||||
new BaseListPopupStep<Language>("Choose Language", languages) {
|
||||
@NotNull
|
||||
@@ -110,7 +141,7 @@ public class NewScratchFileAction extends AnAction implements DumbAware {
|
||||
return associatedLanguage != null ? associatedLanguage.getIcon() : null;
|
||||
}
|
||||
};
|
||||
step.setDefaultOptionIndex(Math.max(0, languages.indexOf(ObjectUtils.chooseNotNull(previous, StdLanguages.TEXT))));
|
||||
step.setDefaultOptionIndex(0);
|
||||
|
||||
return tweakSizeToPreferred(JBPopupFactory.getInstance().createListPopup(step));
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class ScratchWidget extends EditorBasedWidget implements CustomStatusBarWidget.M
|
||||
final LightVirtualFile selectedFile = getScratchFile();
|
||||
if (project == null || editor == null || selectedFile == null) return false;
|
||||
|
||||
ListPopup popup = NewScratchFileAction.buildLanguagePopup(selectedFile.getLanguage(), new Consumer<Language>() {
|
||||
ListPopup popup = NewScratchFileAction.buildLanguagePopup(project, selectedFile.getLanguage(), new Consumer<Language>() {
|
||||
@Override
|
||||
public void consume(Language language) {
|
||||
selectedFile.setLanguage(LanguageSubstitutors.INSTANCE.substituteLanguage(language, selectedFile, myProject));
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class ScratchpadManager {
|
||||
public static ScratchpadManager getInstance(@NotNull Project project) {
|
||||
@@ -29,7 +28,4 @@ public abstract class ScratchpadManager {
|
||||
|
||||
@NotNull
|
||||
public abstract VirtualFile createScratchFile(@NotNull Language language);
|
||||
|
||||
@Nullable
|
||||
public abstract Language getLatestLanguage();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.ide.scratch;
|
||||
|
||||
import com.intellij.ide.util.PropertiesComponent;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
@@ -24,12 +25,14 @@ import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.wm.StatusBar;
|
||||
import com.intellij.openapi.wm.WindowManager;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ScratchpadManagerImpl extends ScratchpadManager implements Disposable {
|
||||
private final Project myProject;
|
||||
private Integer myIndex = 0;
|
||||
private Language myLatestLanguage;
|
||||
|
||||
public ScratchpadManagerImpl(@NotNull Project project) {
|
||||
myProject = project;
|
||||
@@ -45,7 +48,8 @@ public class ScratchpadManagerImpl extends ScratchpadManager implements Disposab
|
||||
@NotNull
|
||||
@Override
|
||||
public VirtualFile createScratchFile(@NotNull final Language language) {
|
||||
myLatestLanguage = language;
|
||||
updateHistory(myProject, language);
|
||||
|
||||
return ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
|
||||
@Override
|
||||
public VirtualFile compute() {
|
||||
@@ -55,16 +59,26 @@ public class ScratchpadManagerImpl extends ScratchpadManager implements Disposab
|
||||
});
|
||||
}
|
||||
|
||||
private static void updateHistory(Project project, Language language) {
|
||||
String[] values = PropertiesComponent.getInstance(project).getValues(ScratchpadManager.class.getName());
|
||||
ArrayList<String> lastUsed = new ArrayList<String>(5);
|
||||
lastUsed.add(language.getID());
|
||||
if (values != null) {
|
||||
for (String value : values) {
|
||||
if (!lastUsed.contains(value)) {
|
||||
lastUsed.add(value);
|
||||
}
|
||||
if (lastUsed.size() == 5) break;
|
||||
}
|
||||
}
|
||||
PropertiesComponent.getInstance(project).setValues(ScratchpadManager.class.getName(), ArrayUtil.toStringArray(lastUsed));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String calculatePrefix(@NotNull Project project) {
|
||||
return project.getLocationHash();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Language getLatestLanguage() {
|
||||
return myLatestLanguage;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String generateFileName() {
|
||||
int updated = myIndex++;
|
||||
|
||||
Reference in New Issue
Block a user