/
RegexpFilter.java
171 lines (138 loc) · 5.71 KB
/
RegexpFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.execution.filters;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Yura Cangea
* @version 1.0
*/
public class RegexpFilter implements Filter, DumbAware {
private static final Logger LOG = Logger.getInstance(RegexpFilter.class);
public static final @NonNls String FILE_PATH_MACROS = "$FILE_PATH$";
public static final @NonNls String LINE_MACROS = "$LINE$";
public static final @NonNls String COLUMN_MACROS = "$COLUMN$";
private static final @NonNls String FILE_PATH_REGEXP = "(^|[\\W])(?<file>(?:\\p{Alpha}\\:|/)[0-9 a-z_A-Z\\-\\\\./]+)";
private static final @NonNls String LINE_REGEXP = "(?<line>[0-9]+)";
private static final @NonNls String COLUMN_REGEXP = "(?<column>[0-9]+)";
private static final @NonNls String FILE_STR = "file";
private static final @NonNls String LINE_STR = "line";
private static final @NonNls String COLUMN_STR = "column";
private final boolean myHasLine;
private final boolean myHasColumn;
private final Pattern myPattern;
private final Project myProject;
public RegexpFilter(Project project, @NonNls String expression) {
myProject = project;
validate(expression);
if (expression.trim().isEmpty()) {
throw new InvalidExpressionException("expression == null or empty");
}
int filePathIndex = expression.indexOf(FILE_PATH_MACROS);
int lineIndex = expression.indexOf(LINE_MACROS);
int columnIndex = expression.indexOf(COLUMN_MACROS);
if (filePathIndex == -1) {
throw new InvalidExpressionException("Expression must contain " + FILE_PATH_MACROS + " macros.");
}
expression = StringUtil.replace(expression, FILE_PATH_MACROS, FILE_PATH_REGEXP);
if (lineIndex != -1) {
expression = StringUtil.replace(expression, LINE_MACROS, LINE_REGEXP);
myHasLine = true;
} else {
myHasLine = false;
}
if (columnIndex != -1) {
expression = StringUtil.replace(expression, COLUMN_MACROS, COLUMN_REGEXP);
myHasColumn = true;
} else {
myHasColumn = false;
}
myPattern = Pattern.compile(expression, Pattern.MULTILINE);
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void validate(String expression) {
if (StringUtil.isEmptyOrSpaces(expression)) throw new InvalidExpressionException("expression == null or empty");
expression = substituteMacrosWithRegexps(expression);
Pattern.compile(expression, Pattern.MULTILINE);
}
public Pattern getPattern() {
return myPattern;
}
private static String substituteMacrosWithRegexps(String expression) {
int filePathIndex = expression.indexOf(FILE_PATH_MACROS);
int lineIndex = expression.indexOf(LINE_MACROS);
int columnIndex = expression.indexOf(COLUMN_MACROS);
if (filePathIndex == -1) {
throw new InvalidExpressionException("Expression must contain " + FILE_PATH_MACROS + " macros.");
}
expression = StringUtil.replace(expression, FILE_PATH_MACROS, FILE_PATH_REGEXP);
if (lineIndex != -1) {
expression = StringUtil.replace(expression, LINE_MACROS, LINE_REGEXP);
}
if (columnIndex != -1) {
expression = StringUtil.replace(expression, COLUMN_MACROS, COLUMN_REGEXP);
}
return expression;
}
@Override
public Result applyFilter(@NotNull String line, int entireLength) {
Matcher matcher = myPattern.matcher(StringUtil.newBombedCharSequence(line, 100));
try {
if (!matcher.find()) {
return null;
}
}
catch (ProcessCanceledException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Too long matching '" + line + "' by '" + myPattern + "' in " + getClass().getName());
}
return null;
}
String filePath = matcher.group(FILE_STR);
if (filePath == null) {
return null;
}
String lineNumber = "0";
if (myHasLine) {
lineNumber = matcher.group(LINE_STR);
}
String columnNumber = "0";
if (myHasColumn) {
columnNumber = matcher.group(COLUMN_STR);
}
int line1 = 0;
int column = 0;
try {
line1 = Integer.parseInt(lineNumber);
column = Integer.parseInt(columnNumber);
}
catch (NumberFormatException e) {
// Do nothing, so that line and column will remain at their initial zero values.
}
if (line1 > 0) line1 -= 1;
if (column > 0) column -= 1;
// Calculate the offsets relative to the entire text.
final int highlightStartOffset = entireLength - line.length() + matcher.start(FILE_STR);
final int highlightEndOffset = highlightStartOffset + filePath.length();
final HyperlinkInfo info = createOpenFileHyperlink(filePath, line1, column);
return new Result(highlightStartOffset, highlightEndOffset, info);
}
protected @Nullable HyperlinkInfo createOpenFileHyperlink(String fileName, final int line, final int column) {
fileName = fileName.replace(File.separatorChar, '/');
VirtualFile file = LocalFileSystem.getInstance().findFileByPathIfCached(fileName);
return file != null ? new OpenFileHyperlinkInfo(myProject, file, line, column) : null;
}
public static String[] getMacrosName() {
return new String[]{FILE_PATH_MACROS, LINE_MACROS, COLUMN_MACROS};
}
}