下面列出了com.intellij.psi.PsiCompiledElement#com.intellij.lang.surroundWith.Surrounder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void doTest(final Surrounder surrounder) throws Exception {
myFixture.configureByFile(getTestName(false) + ".hx");
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
SurroundWithHandler.invoke(getProject(), myFixture.getEditor(), myFixture.getFile(), surrounder);
PsiDocumentManager.getInstance(getProject()).doPostponedOperationsAndUnblockDocument(myFixture.getDocument(myFixture.getFile()));
CodeStyleManager.getInstance(myFixture.getProject()).reformat(myFixture.getFile());
}
});
/*CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
SurroundWithHandler.invoke(getProject(), myFixture.getEditor(), myFixture.getFile(), surrounder);
PsiDocumentManager.getInstance(getProject()).doPostponedOperationsAndUnblockDocument(myFixture.getDocument(myFixture.getFile()));
CodeStyleManager.getInstance(myFixture.getProject()).reformat(myFixture.getFile());
}
}, null, null);*/
myFixture.checkResultByFile(getTestName(false) + "_after.hx");
}
private static void invokeSurrounderInTests(Project project,
Editor editor,
PsiFile file,
Surrounder surrounder,
int startOffset,
int endOffset, List<SurroundDescriptor> surroundDescriptors) {
assert ApplicationManager.getApplication().isUnitTestMode();
for (SurroundDescriptor descriptor : surroundDescriptors) {
final PsiElement[] elements = descriptor.getElementsToSurround(file, startOffset, endOffset);
if (elements.length > 0) {
for (Surrounder descriptorSurrounder : descriptor.getSurrounders()) {
if (surrounder.getClass().equals(descriptorSurrounder.getClass())) {
doSurround(project, editor, surrounder, elements);
return;
}
}
}
}
}
@NotNull
@Override
public Surrounder[] getSurrounders() {
return new Surrounder[]{
new HaxeIfSurrounder(),
new HaxeIfElseSurrounder(),
new HaxeWhileSurrounder(),
new HaxeDoWhileSurrounder(),
new HaxeTryCatchSurrounder()
};
}
public static void doSurroundWithTest(@Nonnull final CodeInsightTestFixture fixture, @Nonnull final Surrounder surrounder,
@Nonnull final String before, @Nonnull final String after) {
fixture.configureByFile(before);
new WriteCommandAction.Simple(fixture.getProject()) {
@Override
protected void run() throws Throwable {
SurroundWithHandler.invoke(fixture.getProject(), fixture.getEditor(), fixture.getFile(), surrounder);
}
}.execute();
fixture.checkResultByFile(after, false);
}
public static void invoke(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file, Surrounder surrounder) {
if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
if (file instanceof PsiCompiledElement) {
HintManager.getInstance().showErrorHint(editor, "Can't modify decompiled code");
return;
}
List<AnAction> applicable = buildSurroundActions(project, editor, file, surrounder);
if (applicable != null) {
showPopup(editor, applicable);
}
else if (!ApplicationManager.getApplication().isUnitTestMode()) {
HintManager.getInstance().showErrorHint(editor, "Couldn't find Surround With variants applicable to the current context");
}
}
static void doSurround(final Project project, final Editor editor, final Surrounder surrounder, final PsiElement[] elements) {
if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
return;
}
try {
PsiDocumentManager.getInstance(project).commitAllDocuments();
int col = editor.getCaretModel().getLogicalPosition().column;
int line = editor.getCaretModel().getLogicalPosition().line;
if (!editor.getCaretModel().supportsMultipleCarets()) {
LogicalPosition pos = new LogicalPosition(0, 0);
editor.getCaretModel().moveToLogicalPosition(pos);
}
TextRange range = surrounder.surroundElements(project, editor, elements);
if (range != CARET_IS_OK) {
if (TemplateManager.getInstance(project).getActiveTemplate(editor) == null &&
InplaceRefactoring.getActiveInplaceRenamer(editor) == null) {
LogicalPosition pos1 = new LogicalPosition(line, col);
editor.getCaretModel().moveToLogicalPosition(pos1);
}
if (range != null) {
int offset = range.getStartOffset();
editor.getCaretModel().removeSecondaryCarets();
editor.getCaretModel().moveToOffset(offset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
}
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
public InvokeSurrounderAction(Surrounder surrounder, Project project, Editor editor, PsiElement[] elements, char mnemonic) {
super(UIUtil.MNEMONIC + String.valueOf(mnemonic) + ". " + surrounder.getTemplateDescription());
mySurrounder = surrounder;
myProject = project;
myEditor = editor;
myElements = elements;
}
@Nullable
public static TextRange surround(@Nonnull Surrounder surrounder,
@Nonnull Editor editor,
@Nonnull PsiElement expr) {
Project project = expr.getProject();
PsiElement[] elements = {expr};
if (surrounder.isApplicable(elements)) {
return surrounder.surroundElements(project, editor, elements);
}
else {
showErrorHint(project, editor);
}
return null;
}
@NotNull
public Surrounder[] getSurrounders() {
return new Surrounder[] {
new IfSurrounder(),
new ParensSurrounder(),
new CStyleCastSurrounder(),
new ConstCastSurrounder(),
new DynamicCastSurrounder(),
new ReinterpretCastSurrounder(),
new StaticCastSurrounder()
};
}
@NotNull
@Override
protected Surrounder getSurrounder() {
return new JavaWithIfElseExpressionSurrounder();
}
@NotNull
@Override
protected Surrounder getSurrounder() {
return new JavaWithIfElseIfExpressionSurrounder();
}
@Nonnull
@Override
public Surrounder[] getSurrounders()
{
return mySurrounders;
}
@Nonnull
@Override
public Surrounder[] getSurrounders() {
return SURROUNDERS;
}
@Nullable
public static List<AnAction> buildSurroundActions(final Project project, final Editor editor, PsiFile file, @Nullable Surrounder surrounder){
SelectionModel selectionModel = editor.getSelectionModel();
boolean hasSelection = selectionModel.hasSelection();
if (!hasSelection) {
selectionModel.selectLineAtCaret();
}
int startOffset = selectionModel.getSelectionStart();
int endOffset = selectionModel.getSelectionEnd();
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiElement element1 = file.findElementAt(startOffset);
PsiElement element2 = file.findElementAt(endOffset - 1);
if (element1 == null || element2 == null) return null;
TextRange textRange = new TextRange(startOffset, endOffset);
for(SurroundWithRangeAdjuster adjuster: Extensions.getExtensions(SurroundWithRangeAdjuster.EP_NAME)) {
textRange = adjuster.adjustSurroundWithRange(file, textRange, hasSelection);
if (textRange == null) return null;
}
startOffset = textRange.getStartOffset();
endOffset = textRange.getEndOffset();
element1 = file.findElementAt(startOffset);
final Language baseLanguage = file.getViewProvider().getBaseLanguage();
assert element1 != null;
final Language l = element1.getParent().getLanguage();
List<SurroundDescriptor> surroundDescriptors = new ArrayList<SurroundDescriptor>();
surroundDescriptors.addAll(LanguageSurrounders.INSTANCE.allForLanguage(l));
if (l != baseLanguage) surroundDescriptors.addAll(LanguageSurrounders.INSTANCE.allForLanguage(baseLanguage));
surroundDescriptors.add(CustomFoldingSurroundDescriptor.INSTANCE);
int exclusiveCount = 0;
List<SurroundDescriptor> exclusiveSurroundDescriptors = new ArrayList<SurroundDescriptor>();
for (SurroundDescriptor sd : surroundDescriptors) {
if (sd.isExclusive()) {
exclusiveCount++;
exclusiveSurroundDescriptors.add(sd);
}
}
if (exclusiveCount > 0) {
surroundDescriptors = exclusiveSurroundDescriptors;
}
if (surrounder != null) {
invokeSurrounderInTests(project, editor, file, surrounder, startOffset, endOffset, surroundDescriptors);
return null;
}
Map<Surrounder, PsiElement[]> surrounders = ContainerUtil.newLinkedHashMap();
for (SurroundDescriptor descriptor : surroundDescriptors) {
final PsiElement[] elements = descriptor.getElementsToSurround(file, startOffset, endOffset);
if (elements.length > 0) {
for (PsiElement element : elements) {
assert element != null : "descriptor " + descriptor + " returned null element";
assert element.isValid() : descriptor;
}
for (Surrounder s: descriptor.getSurrounders()) {
surrounders.put(s, elements);
}
}
}
return doBuildSurroundActions(project, editor, file, surrounders);
}
@Nullable
private static List<AnAction> doBuildSurroundActions(Project project,
Editor editor,
PsiFile file,
Map<Surrounder, PsiElement[]> surrounders) {
final List<AnAction> applicable = new ArrayList<AnAction>();
boolean hasEnabledSurrounders = false;
Set<Character> usedMnemonicsSet = new HashSet<Character>();
int index = 0;
for (Map.Entry<Surrounder, PsiElement[]> entry : surrounders.entrySet()) {
Surrounder surrounder = entry.getKey();
PsiElement[] elements = entry.getValue();
if (surrounder.isApplicable(elements)) {
char mnemonic;
if (index < 9) {
mnemonic = (char)('0' + index + 1);
}
else if (index == 9) {
mnemonic = '0';
}
else {
mnemonic = (char)('A' + index - 10);
}
index++;
usedMnemonicsSet.add(Character.toUpperCase(mnemonic));
applicable.add(new InvokeSurrounderAction(surrounder, project, editor, elements, mnemonic));
hasEnabledSurrounders = true;
}
}
List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
if (!templates.isEmpty() || !customTemplates.isEmpty()) {
applicable.add(new AnSeparator("Live templates"));
}
for (TemplateImpl template : templates) {
applicable.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet));
hasEnabledSurrounders = true;
}
for (CustomLiveTemplate customTemplate : customTemplates) {
applicable.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet));
hasEnabledSurrounders = true;
}
if (!templates.isEmpty() || !customTemplates.isEmpty()) {
applicable.add(AnSeparator.getInstance());
applicable.add(new ConfigureTemplatesAction());
}
return hasEnabledSurrounders ? applicable : null;
}
@Nonnull
protected abstract Surrounder getSurrounder();