prefix restoring naturally goes to the InsertedSingleItem phase

This commit is contained in:
peter
2011-01-25 21:03:43 +01:00
parent fda0008b9d
commit 66b68e1148
4 changed files with 15 additions and 27 deletions
@@ -121,10 +121,10 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
psiFile.putUserData(PsiFileEx.BATCH_REFERENCE_PROCESSING, Boolean.TRUE);
CompletionProgressIndicator indicator = CompletionServiceImpl.getCompletionService().getCurrentCompletion();
CompletionPhase phase = CompletionServiceImpl.getCompletionPhase();
if (indicator != null) {
indicator.closeAndFinish(false);
} else {
CompletionPhase phase = CompletionServiceImpl.getCompletionPhase();
if (phase instanceof CompletionPhase.ZombiePhase) {
indicator = ((CompletionPhase.ZombiePhase)phase).indicator;
}
@@ -140,7 +140,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
if (repeated) {
time = Math.max(indicator.getParameters().getInvocationCount() + 1, 2);
indicator.restorePrefix();
indicator.restorePrefix(phase);
}
}
@@ -495,7 +495,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
else if (decision instanceof AutoCompletionDecision.InsertItem) {
final LookupElement item = ((AutoCompletionDecision.InsertItem)decision).getElement();
indicator.closeAndFinish(true);
indicator.rememberDocumentState();
final Runnable restorePrefix = indicator.rememberDocumentState();
indicator.getOffsetMap()
.addOffset(CompletionInitializationContext.START_OFFSET, (offset1 - item.getPrefixMatcher().getPrefix().length()));
handleSingleItem(offset2, indicator, items, item.getLookupString(), item);
@@ -503,7 +503,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
// the insert handler may have started a live template with completion
if (CompletionService.getCompletionService().getCurrentCompletion() == null &&
!ApplicationManager.getApplication().isUnitTestMode()) {
CompletionServiceImpl.setCompletionPhase(new CompletionPhase.InsertedSingleItem(indicator));
CompletionServiceImpl.setCompletionPhase(new CompletionPhase.InsertedSingleItem(indicator, restorePrefix));
assert indicator.getCompletionState().isWaitingAfterAutoInsertion();
}
}
@@ -103,8 +103,11 @@ public abstract class CompletionPhase implements Disposable {
}
public static class InsertedSingleItem extends ZombiePhase {
public InsertedSingleItem(CompletionProgressIndicator indicator) {
public final Runnable restorePrefix;
public InsertedSingleItem(CompletionProgressIndicator indicator, Runnable restorePrefix) {
super(null, indicator);
this.restorePrefix = restorePrefix;
}
}
public static class NoSuggestionsHint extends ZombiePhase {
@@ -539,13 +539,15 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
return aBoolean.booleanValue();
}
public void restorePrefix() {
public void restorePrefix(final CompletionPhase zombie) {
new WriteCommandAction(getProject(), getCompletionCommandName()) {
@Override
protected void run(Result result) throws Throwable {
setMergeCommand();
myState.restorePrefix();
if (zombie instanceof CompletionPhase.InsertedSingleItem) {
((CompletionPhase.InsertedSingleItem)zombie).restorePrefix.run();
}
getLookup().restorePrefix();
}
}.execute();
@@ -556,7 +558,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
return myEditor;
}
public void rememberDocumentState() {
public Runnable rememberDocumentState() {
myState.assertDisposed();
assert !myState.areModifiersChanged() : myState;
@@ -565,7 +567,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
final int selStart = myEditor.getSelectionModel().getSelectionStart();
final int selEnd = myEditor.getSelectionModel().getSelectionEnd();
myState.setRestorePrefix(new Runnable() {
return new Runnable() {
@Override
public void run() {
DocumentEx document = (DocumentEx) myEditor.getDocument();
@@ -574,7 +576,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
document.replaceString(caret, document.getTextLength(), documentText.substring(caret));
myEditor.getSelectionModel().setSelection(selStart, selEnd);
}
});
};
}
public boolean isRepeatedInvocation(CompletionType completionType, Editor editor) {
@@ -14,7 +14,6 @@ public class CompletionState {
private Boolean myToRestart;
private boolean myRestartScheduled;
private boolean myModifiersChanged;
private Runnable myRestorePrefix;
private boolean myBackgrounded;
private volatile boolean myFocusLookupWhenDone;
private volatile int myCount;
@@ -73,17 +72,11 @@ public class CompletionState {
public boolean isWaitingAfterAutoInsertion() {
CompletionPhase phase = CompletionServiceImpl.getCompletionPhase();
if (phase instanceof CompletionPhase.InsertedSingleItem && ((CompletionPhase.InsertedSingleItem)phase).indicator.getCompletionState() == this) {
LOG.assertTrue(myRestorePrefix != null, this);
return true;
}
return false;
}
public void setRestorePrefix(Runnable restorePrefix) {
CompletionServiceImpl.assertPhase(CompletionPhase.NoCompletion.getClass());
myRestorePrefix = restorePrefix;
}
public boolean isBackgrounded() {
ApplicationManager.getApplication().assertIsDispatchThread();
return myBackgrounded;
@@ -106,18 +99,9 @@ public class CompletionState {
LOG.assertTrue(myCompletionDisposed, this);
}
public void restorePrefix() {
CompletionServiceImpl.assertPhase(CompletionPhase.NoCompletion.getClass());
if (myRestorePrefix != null) {
myRestorePrefix.run();
myRestorePrefix = null;
}
}
public void handleDeath() {
ApplicationManager.getApplication().assertIsDispatchThread();
assertDisposed();
myRestorePrefix = null;
}
int incCount() {
@@ -137,7 +121,6 @@ public class CompletionState {
", myToRestart=" + myToRestart +
", myRestartScheduled=" + myRestartScheduled +
", myModifiersReleased=" + myModifiersChanged +
", myRestorePrefix=" + myRestorePrefix +
", myBackgrounded=" + myBackgrounded +
", myFocusLookupWhenDone=" + myFocusLookupWhenDone +
", myCount=" + myCount +