No custom code completion when embedding html editor?

Hi I am trying to embed the intellij html editor into my own dialog. So far I have it working, but I only get syntax coloring but no code completion.

 

Here is the code snippet:

 

Editor editor = editorFactory.createEditor(document, project, 
FileTypeManager.getInstance().getFileTypeByExtension(".html"), false);

final EditorSettings editorSettings = editor.getSettings();
editorSettings.setLineMarkerAreaShown(true);
editorSettings.setLineNumbersShown(true);
editorSettings.setFoldingOutlineShown(true);
editorSettings.setAnimatedScrolling(true);
editorSettings.setWheelFontChangeEnabled(true);
editorSettings.setVariableInplaceRenameEnabled(true);
editorSettings.setDndEnabled(true);
editorSettings.setAutoCodeFoldingEnabled(true);
editorSettings.setSmartHome(true);

mainForm.getPnEditorHolder().getViewport().setView(editor.getComponent());



So my question is what am I doing wrong here? Editing works but I would love
to have also the advanced features of this editor enabled.

11 comments
Comment actions Permalink
Official comment

First guess: are you showing that dialog in a write action? If yes, then please don't.

Second guess: is the dialog shown from within SwingUtilities.invokeLater or equivalent call? Then please change to Application#invokeLater with default modality state.

Otherwise, I'd recommend debugging it, starting with a breakpoint at CompletionAutoPopupHandler#checkAutoPopup and stepping through the logic.

Comment actions Permalink

I call it straight from the actionPerformed method.

 

0
Comment actions Permalink

I stripped the code down to the bare minimum (aka removed the dialog wrapper and everything else including the settings and just pushed the generated editor into a plain swing window:

public class CreateTnDecComponent extends AnAction {


public CreateTnDecComponent() {
super();
}


@Override
public void actionPerformed(AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);

final EditorFactory editorFactory = EditorFactory.getInstance();
Document document = editorFactory.createDocument("");
final Editor editor = editorFactory.createEditor(document, project, FileTypeManager.getInstance().getFileTypeByExtension(".html"), false);


JDialog frame = new JDialog();

frame.setContentPane(editor.getComponent());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();

frame.setVisible(true);

}
}

 

unfortunately the result is the same, I get syntax coloring and not autocompletion. 

CompletionAutoPopupHandler#checkAutoPopup is not called at all.

 

0
Comment actions Permalink

Ok found it, the problem is that basically the document has not an assigned file type

following code works:

 

//Document document = editorFactory.createDocument("");
VirtualFile vfile = null;
try {
vfile = project.getBaseDir().createChildData(project, "__test.html");
} catch (IOException e) {
e.printStackTrace();
}
Document document = FileDocumentManager.getInstance().getDocument(vfile);


And voila the code completion triggers as expected. The question is whether I can enforce the html
filetype info on the rootless document. I am not very eager to create a vfile every time.

0
Comment actions Permalink

Please try "PsiFileFactory#createFileFromText" and then "file.getViewProvider().getDocument()"

0
Comment actions Permalink

Tried it, did not work out again the editor now is showing up without any code completion. I will simply create a temporary file and live with it.

 

0
Comment actions Permalink

Actually half a step forward, the code completion on html tags now works, but the completion popup does not show up.

also code folding does not seem to work.

 

0
Comment actions Permalink

Actually the best solution i could find so far is to create a temp file via the FileUtils with an html extension and then push this into a virtual file and from that one create the document. That way everything works and the temp file does not mess into your own project.

 

0
Comment actions Permalink

Creating a temp file on disk doesn't seem to be a good idea in general. If you provide standalone code that doesn't work, I could try to figure out what's wrong.

0
Comment actions Permalink

I will do that later today, thanks for investigating

 

0
Comment actions Permalink

I found it, you have to use

 

  • PsiFileFactory#createFileFromText(fileName, HTMLLanguage.INSTANCE, "..."

 

instead of 

 

  • PsiFileFactory#createFileFromText(fileName, HTMLFileType.INSTANCE, "..."

 

Apparently the editor switches to some reduced mode if you create a document from a psi file which just has a html file type. 

0

Please sign in to leave a comment.