|
17 | 17 |
|
18 | 18 | import com.intellij.java.language.impl.JavaFileType; |
19 | 19 | import com.intellij.java.language.impl.codeInsight.template.JavaTemplateUtil; |
| 20 | +import consulo.annotation.access.RequiredReadAction; |
20 | 21 | import consulo.annotation.access.RequiredWriteAction; |
21 | 22 | import consulo.fileTemplate.FileTemplate; |
22 | 23 | import consulo.fileTemplate.FileTemplateManager; |
23 | 24 | import consulo.fileTemplate.FileTemplateUtil; |
24 | 25 | import consulo.language.editor.inspection.LocalQuickFix; |
25 | 26 | import consulo.language.editor.inspection.ProblemDescriptor; |
26 | | -import consulo.language.editor.inspection.ProblemHighlightType; |
27 | 27 | import consulo.language.editor.inspection.localize.InspectionLocalize; |
28 | 28 | import consulo.language.editor.inspection.scheme.InspectionManager; |
29 | 29 | import consulo.language.psi.PsiComment; |
|
47 | 47 | * @author cdr |
48 | 48 | */ |
49 | 49 | public class FileHeaderChecker { |
50 | | - private static final Logger LOG = Logger.getInstance(FileHeaderChecker.class); |
51 | | - |
52 | | - static ProblemDescriptor checkFileHeader(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean onTheFly) { |
53 | | - IntObjectMap<String> offsetToProperty = IntMaps.newIntObjectHashMap(); |
54 | | - FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME); |
55 | | - Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty); |
56 | | - Matcher matcher = pattern.matcher(file.getViewProvider().getContents()); |
57 | | - if (!matcher.matches()) { |
58 | | - return null; |
59 | | - } |
60 | | - |
61 | | - PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class); |
62 | | - if (element == null) { |
63 | | - return null; |
64 | | - } |
65 | | - |
66 | | - LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty, file.getProject(), onTheFly); |
67 | | - String description = InspectionLocalize.defaultFileTemplateDescription().get(); |
68 | | - return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
69 | | - } |
70 | | - |
| 50 | + private static final Logger LOG = Logger.getInstance(FileHeaderChecker.class); |
| 51 | + |
| 52 | + @RequiredReadAction |
| 53 | + static ProblemDescriptor checkFileHeader(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean onTheFly) { |
| 54 | + IntObjectMap<String> offsetToProperty = IntMaps.newIntObjectHashMap(); |
| 55 | + FileTemplate defaultTemplate = |
| 56 | + FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME); |
| 57 | + Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty); |
| 58 | + Matcher matcher = pattern.matcher(file.getViewProvider().getContents()); |
| 59 | + if (!matcher.matches()) { |
| 60 | + return null; |
| 61 | + } |
71 | 62 |
|
72 | | - private static Properties computeProperties(final Matcher matcher, final IntObjectMap<String> offsetToProperty, Project project) { |
73 | | - Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties()); |
| 63 | + PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class); |
| 64 | + if (element == null) { |
| 65 | + return null; |
| 66 | + } |
74 | 67 |
|
75 | | - int[] offsets = offsetToProperty.keys(); |
76 | | - Arrays.sort(offsets); |
77 | | - for (int i = 0; i < offsets.length; i++) { |
78 | | - final int offset = offsets[i]; |
79 | | - String propName = offsetToProperty.get(offset); |
80 | | - int groupNum = i + 2; // first group is whole doc comment |
81 | | - String propValue = matcher.group(groupNum); |
82 | | - properties.setProperty(propName, propValue); |
| 68 | + return manager.newProblemDescriptor(InspectionLocalize.defaultFileTemplateDescription()) |
| 69 | + .range(element) |
| 70 | + .onTheFly(onTheFly) |
| 71 | + .withFixes(createQuickFixes(matcher, offsetToProperty, file.getProject(), onTheFly)) |
| 72 | + .create(); |
83 | 73 | } |
84 | 74 |
|
85 | | - return properties; |
86 | | - } |
87 | | - |
88 | | - private static LocalQuickFix[] createQuickFix(final Matcher matcher, final IntObjectMap<String> offsetToProperty, Project project, boolean onTheFly) { |
89 | | - final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME); |
90 | | - |
91 | | - ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() { |
92 | | - @Override |
93 | | - @RequiredWriteAction |
94 | | - public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) { |
95 | | - PsiElement element = descriptor.getPsiElement(); |
96 | | - if (element == null) { |
97 | | - return; |
| 75 | + private static Properties computeProperties(Matcher matcher, IntObjectMap<String> offsetToProperty, Project project) { |
| 76 | + Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties()); |
| 77 | + |
| 78 | + int[] offsets = offsetToProperty.keys(); |
| 79 | + Arrays.sort(offsets); |
| 80 | + for (int i = 0; i < offsets.length; i++) { |
| 81 | + int offset = offsets[i]; |
| 82 | + String propName = offsetToProperty.get(offset); |
| 83 | + int groupNum = i + 2; // first group is whole doc comment |
| 84 | + String propValue = matcher.group(groupNum); |
| 85 | + properties.setProperty(propName, propValue); |
98 | 86 | } |
99 | 87 |
|
100 | | - String newText; |
101 | | - try { |
102 | | - newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim(); |
103 | | - } catch (IOException e) { |
104 | | - LOG.error(e); |
105 | | - return; |
106 | | - } |
| 88 | + return properties; |
| 89 | + } |
107 | 90 |
|
108 | | - if (!newText.isEmpty()) { |
109 | | - PsiElement parent = element.getParent(); |
110 | | - PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText); |
111 | | - for (PsiElement child : tempFile.getChildren()) { |
112 | | - if (child.getTextLength() > 0) { |
113 | | - parent.addBefore(child, element); |
| 91 | + private static LocalQuickFix[] createQuickFixes( |
| 92 | + final Matcher matcher, |
| 93 | + final IntObjectMap<String> offsetToProperty, |
| 94 | + Project project, |
| 95 | + boolean onTheFly |
| 96 | + ) { |
| 97 | + final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME); |
| 98 | + |
| 99 | + ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() { |
| 100 | + @Override |
| 101 | + @RequiredWriteAction |
| 102 | + public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) { |
| 103 | + PsiElement element = descriptor.getPsiElement(); |
| 104 | + if (element == null) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + String newText; |
| 109 | + try { |
| 110 | + newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim(); |
| 111 | + } |
| 112 | + catch (IOException e) { |
| 113 | + LOG.error(e); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + if (!newText.isEmpty()) { |
| 118 | + PsiElement parent = element.getParent(); |
| 119 | + PsiFile tempFile = |
| 120 | + PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText); |
| 121 | + for (PsiElement child : tempFile.getChildren()) { |
| 122 | + if (child.getTextLength() > 0) { |
| 123 | + parent.addBefore(child, element); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + element.delete(); |
114 | 129 | } |
115 | | - } |
116 | | - } |
| 130 | + }; |
117 | 131 |
|
118 | | - element.delete(); |
119 | | - } |
120 | | - }; |
121 | | - |
122 | | - if (onTheFly) { |
123 | | - LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix); |
124 | | - return template.isDefault() ? new LocalQuickFix[]{editFileTemplateFix} : new LocalQuickFix[]{ |
125 | | - replaceTemplateFix, |
126 | | - editFileTemplateFix |
127 | | - }; |
| 132 | + if (onTheFly) { |
| 133 | + LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix); |
| 134 | + return template.isDefault() |
| 135 | + ? new LocalQuickFix[]{editFileTemplateFix} |
| 136 | + : new LocalQuickFix[]{replaceTemplateFix, editFileTemplateFix}; |
| 137 | + } |
| 138 | + return template.isDefault() ? LocalQuickFix.EMPTY_ARRAY : new LocalQuickFix[]{replaceTemplateFix}; |
128 | 139 | } |
129 | | - return template.isDefault() ? null : new LocalQuickFix[]{replaceTemplateFix}; |
130 | | - } |
131 | 140 | } |
0 commit comments