<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.codeInsight.template.macro;

import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.template.*;
import com.intellij.codeInsight.template.Result;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.openapi.application.*;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

public abstract class BaseCompleteMacro extends Macro {
  private final String myName;

  protected BaseCompleteMacro(@NonNls String name) {
    myName = name;
  }

  @Override
  public String getName() {
    return myName;
  }

  @Override
  public String getPresentableName() {
    return myName + "()";
  }

  @Override
  @NotNull
  public String getDefaultValue() {
    return "a";
  }

  @Override
  public final Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) {
    return new InvokeActionResult(
      new Runnable() {
        @Override
        public void run() {
          invokeCompletion(context);
        }
      }
    );
  }

  private void invokeCompletion(final ExpressionContext context) {
    final Project project = context.getProject();
    final Editor editor = context.getEditor();

    final PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (project.isDisposed() || editor.isDisposed() || psiFile == null || !psiFile.isValid()) return;

        CommandProcessor.getInstance().executeCommand(project, new Runnable() {
          @Override
          public void run() {
            invokeCompletionHandler(project, editor);
            Lookup lookup = LookupManager.getInstance(project).getActiveLookup();

            if (lookup != null) {
              lookup.addLookupListener(new MyLookupListener(context));
            }
            else {
              considerNextTab(editor);
            }
          }
        }, "", null);
      }
    };
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      runnable.run();
    } else {
      ApplicationManager.getApplication().invokeLater(runnable);
    }
  }

  private static void considerNextTab(Editor editor) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null) {
      TextRange range = templateState.getCurrentVariableRange();
      if (range != null && range.getLength() > 0 && editor.getCaretModel().getOffset() == range.getEndOffset()) {
        templateState.nextTab();
      }
    }
  }

  protected abstract void invokeCompletionHandler(Project project, Editor editor);

  private static class MyLookupListener extends LookupAdapter {
    private final ExpressionContext myContext;

    public MyLookupListener(@NotNull ExpressionContext context) {
      myContext = context;
    }

    @Override
    public void itemSelected(LookupEvent event) {
      LookupElement item = event.getItem();
      if (item == null) return;

      char c = event.getCompletionChar();
      if (!LookupEvent.isSpecialCompletionChar(c)) {
        return;
      }

      for(TemplateCompletionProcessor processor: Extensions.getExtensions(TemplateCompletionProcessor.EP_NAME)) {
        if (!processor.nextTabOnItemSelected(myContext, item)) {
          return;
        }
      }

      final Project project = myContext.getProject();
      Runnable runnable = new Runnable() {
        @Override
        public void run() {
          new WriteCommandAction(project) {
            @Override
            protected void run(com.intellij.openapi.application.Result result) throws Throwable {
              Editor editor = myContext.getEditor();
              if (editor != null) {
                considerNextTab(editor);
              }
            }<commit_msg>[^ann] EA-41205 handle outdated template state (cherry-picked from 467fda4)<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.codeInsight.template.macro;

import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.template.*;
import com.intellij.codeInsight.template.Result;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.openapi.application.*;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

public abstract class BaseCompleteMacro extends Macro {
  private final String myName;

  protected BaseCompleteMacro(@NonNls String name) {
    myName = name;
  }

  @Override
  public String getName() {
    return myName;
  }

  @Override
  public String getPresentableName() {
    return myName + "()";
  }

  @Override
  @NotNull
  public String getDefaultValue() {
    return "a";
  }

  @Override
  public final Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) {
    return new InvokeActionResult(
      new Runnable() {
        @Override
        public void run() {
          invokeCompletion(context);
        }
      }
    );
  }

  private void invokeCompletion(final ExpressionContext context) {
    final Project project = context.getProject();
    final Editor editor = context.getEditor();

    final PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (project.isDisposed() || editor.isDisposed() || psiFile == null || !psiFile.isValid()) return;

        CommandProcessor.getInstance().executeCommand(project, new Runnable() {
          @Override
          public void run() {
            invokeCompletionHandler(project, editor);
            Lookup lookup = LookupManager.getInstance(project).getActiveLookup();

            if (lookup != null) {
              lookup.addLookupListener(new MyLookupListener(context));
            }
            else {
              considerNextTab(editor);
            }
          }
        }, "", null);
      }
    };
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      runnable.run();
    } else {
      ApplicationManager.getApplication().invokeLater(runnable);
    }
  }

  private static void considerNextTab(Editor editor) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null) {
      TextRange range = templateState.getCurrentVariableRange();
      if (range != null && range.getLength() > 0 && editor.getCaretModel().getOffset() == range.getEndOffset()) {
        templateState.nextTab();
      }
    }
  }

  protected abstract void invokeCompletionHandler(Project project, Editor editor);

  private static class MyLookupListener extends LookupAdapter {
    private final ExpressionContext myContext;

    public MyLookupListener(@NotNull ExpressionContext context) {
      myContext = context;
    }

    @Override
    public void itemSelected(LookupEvent event) {
      LookupElement item = event.getItem();
      if (item == null) return;

      char c = event.getCompletionChar();
      if (!LookupEvent.isSpecialCompletionChar(c)) {
        return;
      }

      for(TemplateCompletionProcessor processor: Extensions.getExtensions(TemplateCompletionProcessor.EP_NAME)) {
        if (!processor.nextTabOnItemSelected(myContext, item)) {
          return;
        }
      }

      final Project project = myContext.getProject();
      if (project == null) {
        return;
      }
      
      Runnable runnable = new Runnable() {
        @Override
        public void run() {
          new WriteCommandAction(project) {
            @Override
            protected void run(com.intellij.openapi.application.Result result) throws Throwable {
              Editor editor = myContext.getEditor();
              if (editor != null) {
                considerNextTab(editor);
              }
            }
{'fraction_non_alphanumeric': 0.09620458696975848, 'fraction_numerical': 0.0035518571138623907, 'mean_word_length': 3.0841276419394945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093371', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2592, 'n_words': 668}