编写PyCharm扩展以在源代码旁边显示文本(Writing a PyCharm extension to display text beside source code)

编程入门 行业动态 更新时间:2024-10-09 21:21:59
编写PyCharm扩展以在源代码旁边显示文本(Writing a PyCharm extension to display text beside source code)

我写了一个Eclipse PyDev插件,我正在尝试将它移植到PyCharm,但我找不到一种简单的方法来显示Python源代码旁边的文本视图。 有没有比复制PsiAwareTextEditorImpl类及其所有助手更容易的方法?

我让分离器控件工作,所以我可以显示Python源代码旁边的东西。 在这个原型分支中 ,我在Python编辑器旁边显示了一个文本编辑器。 然后我为每个Python文件创建了一个文本文件来保存显示文本,但显示文本文件的名称,管理一堆临时文件会很麻烦。

我逐步完成了标准的编辑器类PsiAwareTextEditorImpl ,发现它有大量的辅助类,并最终调用了EditorPainter.paintTextWithEffects() 。 以下是一些绘制文本的方法:

private void paintTextWithEffects(Graphics2D g, Rectangle clip, int startVisualLine, int endVisualLine) { final CharSequence text = myDocument.getImmutableCharSequence(); // ... VisualLinesIterator visLinesIterator = new VisualLinesIterator(myEditor, startVisualLine); while (!visLinesIterator.atEnd()) { int visualLine = visLinesIterator.getVisualLine(); if (visualLine > endVisualLine || visualLine >= lineCount) break; int y = visLinesIterator.getY(); final boolean paintSoftWraps = paintAllSoftWraps || myEditor.getCaretModel().getLogicalPosition().line == visLinesIterator.getStartLogicalLine(); final int[] currentLogicalLine = new int[] {-1}; paintLineFragments(g, clip, visLinesIterator, y + myView.getAscent(), new LineFragmentPainter() { @Override public void paintBeforeLineStart(Graphics2D g, TextAttributes attributes, int columnEnd, float xEnd, int y) { // ... } @Override public void paint(Graphics2D g, VisualLineFragmentsIterator.Fragment fragment, int start, int end, TextAttributes attributes, float xStart, float xEnd, int y) { // ... } @Override public void paintAfterLineEnd(Graphics2D g, Rectangle clip, IterationState iterationState, int columnStart, float x, int y) { // ... } }); visLinesIterator.advance(); } ComplexTextFragment.flushDrawingCache(g); }

如果我必须重现这一点似乎需要大量工作,那么我是否可以使用一些现有组件来显示不是来自文件的文本块? 我应该创建自己的DocumentImpl实例并以某种方式将其连接到编辑器吗?

这是Eclipse插件左侧的Python代码和右侧的文本显示。

I wrote an Eclipse PyDev plugin, and I'm trying to port it to PyCharm, but I can't find an easy way to display a text view next to the Python source code. Is there any way easier than copying the PsiAwareTextEditorImpl class and all its helpers?

I got the splitter control working so I can display something next to the Python source code. In this prototype branch, I displayed a text editor next to the Python editor. Then I created a text file for each Python file to hold the display text, but that displays the text file's name, and it would be a pain to manage a bunch of temporary files.

I stepped through the standard editor class, PsiAwareTextEditorImpl, and found that it has a ton of helper classes, and eventually calls EditorPainter.paintTextWithEffects(). Here are some of the things it does to paint text:

private void paintTextWithEffects(Graphics2D g, Rectangle clip, int startVisualLine, int endVisualLine) { final CharSequence text = myDocument.getImmutableCharSequence(); // ... VisualLinesIterator visLinesIterator = new VisualLinesIterator(myEditor, startVisualLine); while (!visLinesIterator.atEnd()) { int visualLine = visLinesIterator.getVisualLine(); if (visualLine > endVisualLine || visualLine >= lineCount) break; int y = visLinesIterator.getY(); final boolean paintSoftWraps = paintAllSoftWraps || myEditor.getCaretModel().getLogicalPosition().line == visLinesIterator.getStartLogicalLine(); final int[] currentLogicalLine = new int[] {-1}; paintLineFragments(g, clip, visLinesIterator, y + myView.getAscent(), new LineFragmentPainter() { @Override public void paintBeforeLineStart(Graphics2D g, TextAttributes attributes, int columnEnd, float xEnd, int y) { // ... } @Override public void paint(Graphics2D g, VisualLineFragmentsIterator.Fragment fragment, int start, int end, TextAttributes attributes, float xStart, float xEnd, int y) { // ... } @Override public void paintAfterLineEnd(Graphics2D g, Rectangle clip, IterationState iterationState, int columnStart, float x, int y) { // ... } }); visLinesIterator.advance(); } ComplexTextFragment.flushDrawingCache(g); }

That seems like a ton of work if I have to reproduce that, so is there some existing component that I can use to display a block of text that isn't coming from a file? Should I be creating my own instance of DocumentImpl and somehow wiring that to an editor?

Here's what the Eclipse plugin looks like with the Python code on the left, and the text display on the right.

最满意答案

您可以创建LightVirtualFile实例,填充任何内容并在编辑器中显示为现有类型的文件或您的自定义类型。

You can create instance of LightVirtualFile, fill with any content and display in editor as file of existing type or your custom type.

更多推荐

本文发布于:2023-08-07 17:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源代码   文本   Writing   PyCharm   extension

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!