commitDocument in TypedHandler?

Hi all,

I've created a TypedHandlerDelegate, what I wanted to do was to reindent the line after a certain character was pressed at the start of a line (using CodeStyleManager.adjustLineIndent()). However I was getting errors that the text of the document didn't correspond with the PSI. I managed to fix this by committing the document beforehand - is this correct or is there a better way to do it?

Relevant code snippet:

    if (shouldApplyTo(c, lineText))
    {
      try
      {
        PsiDocumentManager.getInstance(project).commitDocument(document);
        PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());

        CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
        int newPos = codeStyleManager.adjustLineIndent(file, lineStart);


Cheers,
Colin

2 comments
Comment actions Permalink

Hello Colin,

Committing the document in this way is correct. The doPostponedOperations...
call is totally unneeded: it's only needed if you perform some modifications
through the PSI and then need to work with the document.

I've created a TypedHandlerDelegate, what I wanted to do was to
reindent the line after a certain character was pressed at the start
of a line (using CodeStyleManager.adjustLineIndent()). However I was
getting errors that the text of the document didn't correspond with
the PSI. I managed to fix this by committing the document beforehand -
is this correct or is there a better way to do it?

Relevant code snippet:

if (shouldApplyTo(c, lineText))
{
try
{

PsiDocumentManager.getInstance(project).commitDocument(document);

PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnbloc
kDocument(editor.getDocument());
CodeStyleManager codeStyleManager =
CodeStyleManager.getInstance(project);
int newPos = codeStyleManager.adjustLineIndent(file,
lineStart);


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Great, thanks Dmitry.

0

Please sign in to leave a comment.