Filter added to CommandLineState has no effect

Hi, I have a run configuration class that extends RunConfigurationBase (com.intellij.execution.configurations). In the overriden getState method of the class, before returning a CommandLineState, I try to add a filter to it to have some text that matches a regex underlined. The command executes and the process runs and terminates without problems, but the filter has no effect on the output displayed in the console. Here's the source code:

import org.jetbrains.annotations.*;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.configurations.*;
import com.intellij.execution.filters.Filter;
import com.intellij.execution.process.*;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.editor.markup.*;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;

import java.awt.*;
import java.util.regex.*;

public class MyRunConfiguration extends RunConfigurationBase {

protected MyRunConfiguration(Project project, MyConfigurationFactory factory, String name) {
super(project, factory, name);
}

@NotNull
@Override
public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() {
return new MySettingsEditor();
}

@Override
public void checkConfiguration() throws RuntimeConfigurationException {}

@Nullable
@Override
public RunProfileState getState(
@NotNull Executor executor,
@NotNull ExecutionEnvironment executionEnvironment
) throws ExecutionException
{

CommandLineState state = new CommandLineState(executionEnvironment) {

@NotNull
@Override
protected ProcessHandler startProcess() throws ExecutionException {

GeneralCommandLine commandLine = new GeneralCommandLine("/path/to/some/executable");
commandLine.createProcess();

OSProcessHandler processHandler = new OSProcessHandler(commandLine);
processHandler.startNotify();

return processHandler;

}

};

// The added filter has no effect on the output of the console,
// its applyFilter method is never even called!
state.addConsoleFilters(new Filter() {

@Nullable
@Override
public Result applyFilter(String line, int entireLength) {

Matcher matcher = Pattern.compile("[\\w_\\-.]*:[0-9]+:[0-9]+").matcher(line);

if (matcher.matches()) {

MatchResult matchResult = matcher.toMatchResult();

return new Result(matchResult.start(), matchResult.end(), null,
new TextAttributes(null, null, null, EffectType.LINE_UNDERSCORE, Font.PLAIN));

}

return null;

}

});

return state;

}

}

Before anyone says that it's probably simply the regex that is not matching anything in the output, that can't be the reason as the applyFilter method of the added filter is not even being called! Any help would be much appreciated :)

0
3 comments
Avatar
Permanently deleted user

I cannot reproduce the problem with your code, so applyFilter() is being called as expected.

Do you have lines like below in your plugin.xml?

  <extensions defaultExtensionNs="com.intellij">
    <configurationType implementation="some.package.MyRunConfigurationType"/>
</exrtensions>
0
Avatar
Permanently deleted user

Yes I have registered the extension exactly that way :(

Well, if you have tried to reproduce the problem, would you mind posting the exact code you used? (for other classes as well, configuration type, factory, etc.) That way I could compare with my code and hopefully figure out what I'm doing wrong. I'm literally drowning in PyCharm's source code trying to understand how filters are used. Thanks a lot!

0
Avatar
Permanently deleted user

Ok I got it to work by following the pattern used here https://upsource.jetbrains.com/idea-ce/file/idea-ce-d00d8b4ae3ed33097972b8a4286b336bf4ffcfab/python/src/com/jetbrains/python/run/PythonCommandLineState.java.

If anyone else is struggling with filters, the solution for me was basically to also override the execute method of CommandLineState with the following code:

@Override
public ExecutionResult execute(@NotNull Executor executor, @NotNull ProgramRunner runner) throws ExecutionException {

ProcessHandler processHandler = startProcess();

ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(getProject()).getConsole();
consoleView.addMessageFilter(/* Filter anonymous implementation here */);
consoleView.attachToProcess(processHandler);

return new DefaultExecutionResult(consoleView, processHandler);

}

Obviously now the call to state.addConsoleFilters in getState goes away.

0

Please sign in to leave a comment.