TemplateProcessors should properly handle injected files

This commit is contained in:
Alexander Zolotov
2018-08-15 21:01:53 +03:00
parent f70451501a
commit cfe000e8ad
4 changed files with 18 additions and 67 deletions
@@ -1,21 +1,8 @@
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.template.impl;
import com.intellij.codeInsight.template.Template;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
@@ -43,7 +30,8 @@ public class CodeBlockReformattingProcessor implements TemplateOptionalProcessor
if (!template.isToReformat()) return;
PsiDocumentManager.getInstance(project).commitDocument(document);
PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
PsiFile file = editor instanceof EditorWindow ? ((EditorWindow)editor).getInjectedFile()
: PsiUtilBase.getPsiFileInEditor(editor, project);
if (!(file instanceof PsiJavaFile)) return;
CharSequence text = document.getImmutableCharSequence();
@@ -1,22 +1,9 @@
/*
* Copyright 2000-2009 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.template.impl;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.template.Template;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
@@ -36,7 +23,8 @@ public class ShortenFQNamesProcessor implements TemplateOptionalProcessor, DumbA
if (!template.isToShortenLongNames()) return;
PsiDocumentManager.getInstance(project).commitDocument(document);
PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
PsiFile file = editor instanceof EditorWindow ? ((EditorWindow)editor).getInjectedFile()
: PsiUtilBase.getPsiFileInEditor(editor, project);
if (file == null) {
throw new IllegalStateException(editor + " " + editor.getDocument());
}
@@ -1,18 +1,4 @@
/*
* 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.template.impl;
import com.intellij.codeInsight.CodeInsightBundle;
@@ -22,6 +8,7 @@ import com.intellij.codeInsight.template.JavaCodeContextType;
import com.intellij.codeInsight.template.JavaCommentContextType;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateContextType;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
@@ -59,7 +46,8 @@ public class ShortenToStaticImportProcessor implements TemplateOptionalProcessor
}
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
PsiFile file = editor instanceof EditorWindow ? ((EditorWindow)editor).getInjectedFile()
: PsiUtilBase.getPsiFileInEditor(editor, project);
if (file == null) {
return;
}
@@ -1,24 +1,11 @@
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.template;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.impl.TemplateContext;
import com.intellij.codeInsight.template.impl.TemplateOptionalProcessor;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
@@ -46,11 +33,11 @@ public class GroovyShortenFQNamesProcessor implements TemplateOptionalProcessor,
if (!template.isToShortenLongNames()) return;
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
PsiFile file = editor instanceof EditorWindow ? ((EditorWindow)editor).getInjectedFile()
: PsiUtilBase.getPsiFileInEditor(editor, project);
if (file instanceof GroovyFile) {
DumbService.getInstance(project).withAlternativeResolveEnabled(() -> {
JavaCodeStyleManager.getInstance(project).shortenClassReferences(file, templateRange.getStartOffset(),templateRange.getEndOffset());
});
DumbService.getInstance(project).withAlternativeResolveEnabled(() -> JavaCodeStyleManager.getInstance(project)
.shortenClassReferences(file, templateRange.getStartOffset(), templateRange.getEndOffset()));
}
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}