Package com.intellij.coldFusion.UI.editorActions.completionProviders

Source Code of com.intellij.coldFusion.UI.editorActions.completionProviders.CfmlMethodInsertHandler

/*
* Copyright 2000-2013 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.coldFusion.UI.editorActions.completionProviders;

import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.hint.ShowParameterInfoHandler;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.coldFusion.model.psi.CfmlFunction;
import com.intellij.coldFusion.model.psi.CfmlPsiUtil;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;

public class CfmlMethodInsertHandler implements InsertHandler<LookupElement> {
  private static class CfmlMethodInsertHandlerHolder {
    private static final CfmlMethodInsertHandler instance = new CfmlMethodInsertHandler();
  }

  public static CfmlMethodInsertHandler getInstance() {
    return CfmlMethodInsertHandlerHolder.instance;
  }

  protected CfmlMethodInsertHandler() {

  }

  public void handleInsert(InsertionContext context, LookupElement lookupElement) {
    Editor editor = context.getEditor();
    if (CfmlPsiUtil.isFunctionDefinition(lookupElement.getObject())) {
      final CfmlFunction function = CfmlPsiUtil.getFunctionDefinition(lookupElement.getObject());
      if (!CfmlInsertHandlerUtil.isStringAtCaret(editor, "(")) {
        CfmlInsertHandlerUtil.insertStringAtCaret(editor, "()");
        if (function.getParameters().length > 0) {
          editor.getCaretModel().moveCaretRelatively(-1, 0, false, false, true);
          showParameterInfo(editor);
        }
      }
      else {
        if (CfmlInsertHandlerUtil.isStringAtCaret(editor, "()")) {
          editor.getCaretModel().moveCaretRelatively(2, 0, false, false, true);
        }
        else {
          editor.getCaretModel().moveCaretRelatively(1, 0, false, false, true);
          showParameterInfo(editor);
        }
      }
    }
  }

  public static void showParameterInfo(Editor editor) {
    final PsiFile psiFile = PsiDocumentManager.getInstance(editor.getProject()).getPsiFile(editor.getDocument());
    new ShowParameterInfoHandler().invoke(editor.getProject(), editor, psiFile);
  }
}
TOP

Related Classes of com.intellij.coldFusion.UI.editorActions.completionProviders.CfmlMethodInsertHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.