mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
Move ScratchService to API and enable intentions actions in scratches
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package com.intellij.codeInsight.intention;
|
||||
|
||||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.ide.scratch.ScratchFileService;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -51,7 +52,7 @@ public abstract class BaseElementAtCaretIntentionAction extends BaseIntentionAct
|
||||
}
|
||||
|
||||
protected boolean checkFile(@NotNull PsiFile file) {
|
||||
return file.getManager().isInProject(file);
|
||||
return file.getManager().isInProject(file) || ScratchFileService.isInScratchRoot(file.getVirtualFile());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,8 +82,7 @@ public abstract class BaseElementAtCaretIntentionAction extends BaseIntentionAct
|
||||
* @param project the project in which the file is opened.
|
||||
* @param editor the editor for the file.
|
||||
* @param element the element under cursor.
|
||||
* @throws com.intellij.util.IncorrectOperationException
|
||||
*
|
||||
* @throws IncorrectOperationException
|
||||
*/
|
||||
public abstract void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException;
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.intellij.codeInsight.intention;
|
||||
|
||||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.ide.scratch.ScratchFileService;
|
||||
import com.intellij.openapi.editor.CaretModel;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -36,29 +37,31 @@ import org.jetbrains.annotations.Nullable;
|
||||
public abstract class PsiElementBaseIntentionAction extends BaseIntentionAction {
|
||||
@Override
|
||||
public final void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
if (!file.getManager().isInProject(file)) return;
|
||||
if (!checkFile(file)) return;
|
||||
final PsiElement element = getElement(editor, file);
|
||||
if (element != null) {
|
||||
invoke(project, editor, element);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean checkFile(@Nullable PsiFile file) {
|
||||
if (file == null) return false;
|
||||
PsiManager manager = file.getManager();
|
||||
return manager != null && manager.isInProject(file) || ScratchFileService.isInScratchRoot(file.getVirtualFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes intention action for the element under caret.
|
||||
*
|
||||
* @param project the project in which the file is opened.
|
||||
* @param editor the editor for the file.
|
||||
* @param element the element under cursor.
|
||||
*
|
||||
*/
|
||||
public abstract void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException;
|
||||
|
||||
@Override
|
||||
public final boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
if (file == null) return false;
|
||||
final PsiManager manager = file.getManager();
|
||||
if (manager == null) return false;
|
||||
if (!manager.isInProject(file)) return false;
|
||||
if (!checkFile(file)) return false;
|
||||
final PsiElement element = editor == null ? null : getElement(editor, file);
|
||||
return element != null && isAvailable(project, editor, element);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
@@ -95,7 +96,10 @@ public abstract class RootType {
|
||||
public Icon substituteIcon(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
Language language = substituteLanguage(project, file);
|
||||
FileType fileType = LanguageUtil.getLanguageFileType(language);
|
||||
if (fileType == null) fileType = ScratchUtil.getFileTypeFromName(file);
|
||||
if (fileType == null) {
|
||||
String extension = file.getExtension();
|
||||
fileType = extension == null ? null : FileTypeManager.getInstance().getFileTypeByFileName(file.getNameSequence());
|
||||
}
|
||||
return fileType != null ? fileType.getIcon() : null;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.intellij.ide.scratch;
|
||||
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.LanguageFileType;
|
||||
import com.intellij.openapi.fileTypes.PlainTextFileType;
|
||||
import com.intellij.openapi.fileTypes.PlainTextLanguage;
|
||||
@@ -39,7 +38,7 @@ public class ScratchFileType extends LanguageFileType implements FileTypeIdentif
|
||||
|
||||
@Override
|
||||
public boolean isMyFileType(@NotNull VirtualFile file) {
|
||||
return ScratchFileService.getInstance().getRootType(file) != null;
|
||||
return ScratchFileService.isInScratchRoot(file);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -74,7 +74,7 @@ public class ScratchUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static FileType getFileTypeFromName(@NotNull VirtualFile file) {
|
||||
private static FileType getFileTypeFromName(@NotNull VirtualFile file) {
|
||||
String extension = file.getExtension();
|
||||
return extension == null ? null : FileTypeManager.getInstance().getFileTypeByExtension(extension);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user