Custom Editor run code analysis

Answered

Hi JetBrains Support,

 

I created a custom text editor ScpiFileEditor by using the EditorFactory.createEditor(), extended FileEditorProvider and registered it in the plugin xml file. It works well, if I create/open a file with the *.scpi extension, Pycharm uses my ScpiFileEditor. All good so far.

The problem is, if I open the a file in my ScpiFileEditor, the code analysis is stuck on Analyzing... it never gets further.

The funny part is, I tried ScpiFileEditorProvider.getPolicy()  return FileEditorPolicy.PLACE_BEFORE_DEFAULT_EDITOR;

This way, the editor has two tabs - 'SCPI' and 'Text'. If I look at 'SCPI' tab, it shows 'Analyzing...' forever. But If I switch to 'Text', the code analysis runs, and then also the 'SCPI' tab shows the correct analysis results instead of 'Analyzing...'.

It is as-if the analysis on the file must be started somewhere else. What do I have to include/register in my ScpiFileEditor, to have this run properly?

 

Here's the relevant code:

 

ScpiFileEditor class constructor:

public class ScpiFileEditor implements FileEditor
{
private final Editor editor;
private final EditorEx editorEx;
private final VirtualFile myFile;


public ScpiFileEditor(@NotNull VirtualFile myFile, @NotNull Project project)
{
this.myFile = myFile;
var myDocument = FileDocumentManager.getInstance().getDocument(myFile);
//noinspection DataFlowIssue
this.editorEx = (EditorEx)EditorFactory.getInstance().createEditor(myDocument, project, ScpiFileType.INSTANCE,false);
var mm = (EditorMarkupModel)editorEx.getMarkupModel();
mm.setErrorStripeVisible(true);
}

 

ScpiFileEditorProvider code:


public class ScpiFileEditorProvider implements FileEditorProvider, DumbAware
{

@Override
public boolean accept(@NotNull Project project, @NotNull VirtualFile file)
{
var fileType = file.getFileType();
return fileType instanceof ScpiFileType;
}

@Override
public @NotNull FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file)
{
var myDocument = FileDocumentManager.getInstance().getDocument(file);
return new ScpiFileEditor(file, project);
}

@Override
public @NotNull @NonNls String getEditorTypeId()
{
return "SCPI Editor";
}

@Override
public @NotNull FileEditorPolicy getPolicy()
{
return FileEditorPolicy.PLACE_BEFORE_DEFAULT_EDITOR;
}
}

 

Thanks,

Milo

0
8 comments

Hi Milo,

It is unclear what the issue is.

Please share the full code or the minimal example where it can be reproduced with the instructions how to reproduce it. Thanks.

0

Hi Karol,

 

I prepared a minimal IDEA project where it is reproduceable. How do I upload it? The zip has cca 600kB....

 

Milo

0

Hi Karol, uploaded. Upload id: 2023_02_28_eEeS5s12G5b9frHejkFgeU (file: RsIC_example_min.zip)

The zip contains the IDEA project and a test file to open - testfile.scpi

Open it with the built PyCharm, make sure the opened editor is on 'SCPI' tab (at the editor's bottom), and see the code analysis not being performed. Then switch to the 'TEXT', the analysis is performed, and after that, the 'SCPI' tab has the analysis result as well.

I also noticed that the analysis in the 'SCPI' tab is not refreshed after some text changes, you always have to refresh it with the 'TEXT' tab.

 

I hope the explanation is enough to reproduce this.

 

Cheers,

Milo

 

0

This project requires access to custom Maven repository, which requires username and password. Please prepare an example that works out of the box, without any additional configuration or manual steps required.

0

Hi Karol, sorry, I tried to remove all the dependencies, that one I missed.

Here's the new version: Upload id: 2023_02_28_MSzgjzh3HL97sY3e27xMwh (file: RsIC_example.zip)

 

Thanks

Milo

0

Hi Milo,

Please take a look at `FileEditor.getBackgroundHighlighter()`. It seems that you should return a highlighter to invoke analysis. I suggest taking a look at other editor implementations in the https://github.com/JetBrains/intellij-community project, or on https://jb.gg/ipe.

0

Hi Karol, great, thx, got it working when searching for implementations of BackgroundEditorHighlighter.

This you need to override in your FileEditor class:


@Override
public @Nullable BackgroundEditorHighlighter getBackgroundHighlighter()
{
return new TextEditorBackgroundHighlighter(editor.getProject(), editor);
}
0

Please sign in to leave a comment.