how do I use Caretmodel.moveCaretrelatively to locate columns

Answered

hi

I am developing a plugin that supports custom language. I want to achieve fast repair function, create a class, and navigate to the class definition, position the cursor to a certain line, and generate an indentation. I use Caretmodel.moveCaretrelatively. His first parameter Columnshift does not seem to take effect in a blank row. What should I do?

Thanks

3 comments
Comment actions Permalink

Hi,

Please try to add an indent at first. Check:

  • EditorActionUtil.indentLine() - to add indent
  • CodeStyle.getIndentOptions(Project, Document).INDENT_SIZE - to get the indent size for the created document
0
Comment actions Permalink

It is not change, this is my code:

file.getNode().addChild(classDefinition.getNode());
((Navigatable) classDefinition.getLastChild().getNavigationElement()).navigate(true);
Editor selectedTextEditor = FileEditorManager.getInstance(project).getSelectedTextEditor();
CaretModel caretModel = selectedTextEditor.getCaretModel();
caretModel.moveCaretRelatively(0, -1, false, false, false);

int indentSize = CodeStyle.getIndentOptions(project, selectedTextEditor.getDocument()).INDENT_SIZE;
EditorActionUtil.indentLine(project, selectedTextEditor, selectedTextEditor.getDocument().getLineNumber(caretModel.getOffset()), indentSize);
0
Comment actions Permalink

The order is important, so at first add indent, and then move the caret.

0

Please sign in to leave a comment.