Java Code Examples for com.intellij.psi.util.PsiUtilBase#getLanguageInEditor()

The following examples show how to use com.intellij.psi.util.PsiUtilBase#getLanguageInEditor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FindUsagesInFileAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isEnabled(DataContext dataContext) {
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return false;
  }

  Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
  if (editor == null) {
    UsageTarget[] target = dataContext.getData(UsageView.USAGE_TARGETS_KEY);
    return target != null && target.length > 0;
  }
  else {
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file == null) {
      return false;
    }

    Language language = PsiUtilBase.getLanguageInEditor(editor, project);
    if (language == null) {
      language = file.getLanguage();
    }
    return !(LanguageFindUsages.INSTANCE.forLanguage(language) instanceof EmptyFindUsagesProvider);
  }
}
 
Example 2
Source File: CodeCompletionHandlerBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void addCompletionChar(InsertionContext context, LookupElement item) {
  if (!context.getOffsetMap().containsOffset(InsertionContext.TAIL_OFFSET)) {
    String message = "tailOffset<0 after inserting " + item + " of " + item.getClass();
    if (context instanceof WatchingInsertionContext) {
      message += "; invalidated at: " + ((WatchingInsertionContext)context).invalidateTrace + "\n--------";
    }
    LOG.info(message);
  }
  else if (!CompletionAssertions.isEditorValid(context.getEditor())) {
    LOG.info("Injected editor invalidated " + context.getEditor());
  }
  else {
    context.getEditor().getCaretModel().moveToOffset(context.getTailOffset());
  }
  if (context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
    Language language = PsiUtilBase.getLanguageInEditor(context.getEditor(), context.getFile().getProject());
    if (language != null) {
      for (SmartEnterProcessor processor : SmartEnterProcessors.INSTANCE.allForLanguage(language)) {
        if (processor.processAfterCompletion(context.getEditor(), context.getFile())) break;
      }
    }
  }
  else {
    DataContext dataContext = DataManager.getInstance().getDataContext(context.getEditor().getContentComponent());
    EditorActionManager.getInstance().getTypedAction().getHandler().execute(context.getEditor(), context.getCompletionChar(), dataContext);
  }
}
 
Example 3
Source File: CommentByBlockCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Commenter findCommenter(PsiFile file, Editor editor, Caret caret) {
  final FileType fileType = file.getFileType();
  if (fileType instanceof AbstractFileType) {
    return ((AbstractFileType)fileType).getCommenter();
  }

  Language lang = PsiUtilBase.getLanguageInEditor(caret, file.getProject());

  return getCommenter(file, editor, lang, lang);
}
 
Example 4
Source File: TypedHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static FileType getFileType(@Nonnull PsiFile file, @Nonnull Editor editor) {
  FileType fileType = file.getFileType();
  Language language = PsiUtilBase.getLanguageInEditor(editor, file.getProject());
  if (language != null && language != PlainTextLanguage.INSTANCE) {
    LanguageFileType associatedFileType = language.getAssociatedFileType();
    if (associatedFileType != null) fileType = associatedFileType;
  }
  return fileType;
}
 
Example 5
Source File: CompletionInitializationContextImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
CompletionInitializationContextImpl(Editor editor, @Nonnull Caret caret, PsiFile file, CompletionType completionType, int invocationCount) {
  super(editor, caret, PsiUtilBase.getLanguageInEditor(editor, file.getProject()), file, completionType, invocationCount);
  myHostOffsets = new OffsetsInFile(file, getOffsetMap()).toTopLevelFile();
}
 
Example 6
Source File: SmartEnterAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredWriteAction
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null || editor.isOneLineMode()) {
    plainEnter(editor, caret, dataContext);
    return;
  }

  LookupManager.getInstance(project).hideActiveLookup();

  TemplateState state = TemplateManagerImpl.getTemplateState(editor);
  if (state != null) {
    state.gotoEnd();
  }

  final int caretOffset = editor.getCaretModel().getOffset();

  PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
  if (psiFile == null) {
    plainEnter(editor, caret, dataContext);
    return;
  }

  if (EnterAfterUnmatchedBraceHandler.isAfterUnmatchedLBrace(editor, caretOffset, psiFile.getFileType())) {
    EditorActionHandler enterHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
    enterHandler.execute(editor, caret, dataContext);
    return;
  }

  final Language language = PsiUtilBase.getLanguageInEditor(editor, project);
  boolean processed = false;
  if (language != null) {
    final List<SmartEnterProcessor> processors = SmartEnterProcessors.INSTANCE.allForLanguage(language);
    if (!processors.isEmpty()) {
      for (SmartEnterProcessor processor : processors) {
        if (processor.process(project, editor, psiFile)) {
          processed = true;
          break;
        }
      }
    }
  }
  if (!processed) {
    plainEnter(editor, caret, dataContext);
  }
}