CodeStyleManager.adjustLineIndent is throwing Invalid offset detected

Hello,

I have created a new action for my plugin that extracts the selected text from one file, creates a new file and puts the selected text there and then inserts a tag pointing to the newly created file.
Everything is working properly except for when I try to adjust the line indent for the newly created element. The code I'm running looks like this.

ApplicationManager.getApplication().runWriteAction(new Runnable() {
     @Override
     public void run() {
          try {
               CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
               CaretModel caretModel = editor.getCaretModel();
               int caretOffset = selectonModel.getSelectionStart();
               EditorModificationUtil.deleteSelectedText(editor);
               caretModel.moveToOffset(caretOffset);
               int modOffset = EditorModificationUtil.insertStringAtCaret(editor, "<% include " + fileName + " %>", true, false);
               codeStyleManager.reformat(currentFile);
               codeStyleManager.adjustLineIndent(currentFile, caretOffset);
               //caretModel.moveToOffset(caretOffset + modOffset);
               PsiFile createdFile = (PsiFile) elements[0];
               codeStyleManager.reformat(createdFile);
               FileEditorManager.getInstance(project).openFile(currentFile.getVirtualFile(), true);
               TextRange formatRange =  new TextRange(caretOffset,modOffset);
               //codeStyleManager.adjustLineIndent(currentFile, formatRange);
          } catch (Exception e) {
               e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }
     }
});


I'm extending the CreateElementActionBase and running all this code in invokeDialog. No matter what I try the file I'm removing content from doesn't seem to respond to formatting. Formatting in the newly created file works fine (codeStyleManager.reformat(createdFile)).
Do I need to do something with the file before I can format it? It seems like the formatter thinks that the file is longer than it actually is when I look at the thrown exception.

5 comments

Why are you calling both reformat() and adjustLineIndent()? The formatting done by adjustLineIndent() is a subset of the formatting done by reformat(), and adjustLineIndent() will not do anything useful if you call it immediately after reformat().

Also note that reformat() will change the text of the file and invalidate offsets in the text, so after you call reformat(), caretOffset will not point tot the same character that it did before you called reformat(), and may well point beyond the end of the file.

0

Hi Dmitry,

The call to reformat was added after I got errors from adjustLineIndent().
It's correct that both should not be there at the same time.
However the call to reformat has no effect in currentFile at all and

codeStyleManager.adjustLineIndent(currentFile, caretOffset);

always throws the error even if I comment out the reformat.
I have checked the caretOffset that I'm sending in and it seems to be correct.
The offset referred to in the throwable is bigger.
The error I get looks like this "Invalid offset detected (1793). Document length: 1719. Target file: FILE"
The numbers vary depending on the file of course but otherwise I can't find a way around it.
Do I need to somehow save the file or something to adjust the document length before trying the indent?

0

Ok I have found that closing the file, reopening it and then calling adjustLineIndent() works but this feels like a very roundabout way of doing this.
My guess in all of this is that PsiFile doesn't get updated when I call EditorModificationUtil.deleteSelectedText(editor); and then throws an error.

After much searching I found that calling fileDocumentManager.reloadFiles(currentFile.getVirtualFile()); after deleting the text fixes this.
I no longer have to close and reopen the file for this to work.

0

The call you want is PsiDocumentManager.commitDocument(). This will cause the PSI to be rebuilt to reflect the changes you've made directly to the document.

0

Thank you Colin that works like a charm!

0

Please sign in to leave a comment.