Java Code Examples for com.intellij.psi.util.PsiUtilCore#getLanguageAtOffset()

The following examples show how to use com.intellij.psi.util.PsiUtilCore#getLanguageAtOffset() . 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: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int findCodeConstructStartLine(int startLine) {
  DocumentEx document = myEditor.getDocument();
  CharSequence text = document.getImmutableCharSequence();
  int lineStartOffset = document.getLineStartOffset(startLine);
  int firstNonWsOffset = CharArrayUtil.shiftForward(text, lineStartOffset, " \t");
  FileType type = PsiUtilBase.getPsiFileAtOffset(myFile, firstNonWsOffset).getFileType();
  Language language = PsiUtilCore.getLanguageAtOffset(myFile, firstNonWsOffset);
  BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(type, language);
  HighlighterIterator iterator = myEditor.getHighlighter().createIterator(firstNonWsOffset);
  if (braceMatcher.isLBraceToken(iterator, text, type)) {
    int codeConstructStart = braceMatcher.getCodeConstructStart(myFile, firstNonWsOffset);
    return document.getLineNumber(codeConstructStart);
  }
  else {
    return startLine;
  }
}
 
Example 2
Source File: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int findCodeConstructStartLine(int startLine) {
  DocumentEx document = myEditor.getDocument();
  CharSequence text = document.getImmutableCharSequence();
  int lineStartOffset = document.getLineStartOffset(startLine);
  int firstNonWsOffset = CharArrayUtil.shiftForward(text, lineStartOffset, " \t");
  FileType type = PsiUtilBase.getPsiFileAtOffset(myFile, firstNonWsOffset).getFileType();
  Language language = PsiUtilCore.getLanguageAtOffset(myFile, firstNonWsOffset);
  BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(type, language);
  HighlighterIterator iterator = myEditor.getHighlighter().createIterator(firstNonWsOffset);
  if (braceMatcher.isLBraceToken(iterator, text, type)) {
    int codeConstructStart = braceMatcher.getCodeConstructStart(myFile, firstNonWsOffset);
    return document.getLineNumber(codeConstructStart);
  }
  else {
    return startLine;
  }
}
 
Example 3
Source File: BashLiveTemplatesContext.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isInContext(@NotNull PsiFile file, int offset) {
    Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
    if (language.isKindOf(BashFileType.BASH_LANGUAGE)) {
        PsiElement element = file.findElementAt(offset);
        if (element == null) {
            //if a user edits at the end of a comment at the end of a file then findElementAt returns null
            //(for yet unknown reasons)
            element = file.findElementAt(offset - 1);
        }

        return !BashPsiUtils.hasParentOfType(element, PsiComment.class, 3)
                && !BashPsiUtils.hasParentOfType(element, BashShebang.class, 3)
                && !BashPsiUtils.hasParentOfType(element, BashHereDoc.class, 1)
                && !BashPsiUtils.isCommandParameterWord(element);
    }

    return false;
}
 
Example 4
Source File: FlowRenameHandler.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiElement getElement(@Nullable final DataContext context) {
    if (context != null) {
        final Editor editor = CommonDataKeys.EDITOR.getData(context);
        if (editor != null) {
            final int offset = editor.getCaretModel().getOffset();
            final PsiFile file = CommonDataKeys.PSI_FILE.getData(context);
            if (file instanceof XmlFile && MuleConfigUtils.isMuleFile(file)) {
                return file.getViewProvider().findElementAt(offset);
            }
            if (file != null) {
                final Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
                if (language != file.getLanguage()) {
                    final PsiFile psiAtOffset = file.getViewProvider().getPsi(language);
                    if (psiAtOffset instanceof XmlFile && MuleConfigUtils.isMuleFile(psiAtOffset)) {
                        return psiAtOffset.findElementAt(offset);
                    }
                }
            }
        }
    }

    return null;
}
 
Example 5
Source File: TextEditorPsiDataProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static Language getLanguageAtCurrentPositionInEditor(Caret caret, final PsiFile psiFile) {
  int caretOffset = caret.getOffset();
  int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionStart() ||
                                          caretOffset == caret.getSelectionEnd()
                                          ? caret.getSelectionStart()
                                          : caretOffset;
  if (caret.hasSelection()) {
    return getLanguageAtOffset(psiFile, mostProbablyCorrectLanguageOffset, caret.getSelectionEnd());
  }

  return PsiUtilCore.getLanguageAtOffset(psiFile, mostProbablyCorrectLanguageOffset);
}
 
Example 6
Source File: PostfixLiveTemplate.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isApplicable(PsiFile file, int offset, boolean wrapping) {
  PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
  if (wrapping || file == null || settings == null || !settings.isPostfixTemplatesEnabled()) {
    return false;
  }
  Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
  for (PostfixTemplateProvider provider : LanguagePostfixTemplate.LANG_EP.allForLanguage(language)) {
    if (StringUtil.isNotEmpty(computeTemplateKeyWithoutContextChecking(provider, file.getText(), offset + 1))) {
      return true;
    }
  }
  return false;
}
 
Example 7
Source File: PostfixLiveTemplate.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public Set<String> getAllTemplateKeys(PsiFile file, int offset) {
  Set<String> keys = Sets.newHashSet();
  Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
  for (PostfixTemplateProvider provider : LanguagePostfixTemplate.LANG_EP.allForLanguage(language)) {
    keys.addAll(getKeys(provider));
  }
  return keys;
}
 
Example 8
Source File: OverrideMethodsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.OVERRIDE_METHOD.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example 9
Source File: DelegateMethodsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.DELEGATE_METHODS.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example 10
Source File: OverrideMethodsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull final PsiFile file) {
  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.OVERRIDE_METHOD.forLanguage(language);
  if (codeInsightActionHandler != null) {
    return codeInsightActionHandler.isValidFor(editor, file);
  }
  return false;
}
 
Example 11
Source File: DelegateMethodsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull final PsiFile file) {
  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.DELEGATE_METHODS.forLanguage(language);
  if (codeInsightActionHandler != null) {
    return codeInsightActionHandler.isValidFor(editor, file);
  }
  return false;
}
 
Example 12
Source File: ImplementMethodsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.IMPLEMENT_METHOD.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example 13
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Language getLineEndLanguage(@Nonnull PsiFile file, @Nonnull Editor editor, int line) {
  Document document = editor.getDocument();
  int lineEndOffset = document.getLineEndOffset(line) - 1;
  lineEndOffset = Math.max(0, CharArrayUtil.shiftBackward(document.getCharsSequence(), lineEndOffset < 0 ? 0 : lineEndOffset, " \t"));
  return PsiUtilCore.getLanguageAtOffset(file, lineEndOffset);
}
 
Example 14
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Language getLineStartLanguage(@Nonnull Editor editor, @Nonnull PsiFile file, int line) {
  Document document = editor.getDocument();
  int lineStartOffset = document.getLineStartOffset(line);
  lineStartOffset = Math.max(0, CharArrayUtil.shiftForward(document.getCharsSequence(), lineStartOffset, " \t"));
  return PsiUtilCore.getLanguageAtOffset(file, lineStartOffset);
}
 
Example 15
Source File: GotoSuperAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull final Project project, @Nonnull Editor editor, @Nonnull PsiFile file) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  int offset = editor.getCaretModel().getOffset();
  final Language language = PsiUtilCore.getLanguageAtOffset(file, offset);

  final CodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.GOTO_SUPER.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example 16
Source File: ShowExpressionTypeHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
public Map<PsiElement, ExpressionTypeProvider> getExpressions(@Nonnull PsiFile file, @Nonnull Editor editor) {
  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  Set<ExpressionTypeProvider> handlers = getHandlers(file.getProject(), language, file.getViewProvider().getBaseLanguage());
  return getExpressions(file, editor, handlers);
}
 
Example 17
Source File: ImplementMethodsAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull final PsiFile file) {
  final Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.IMPLEMENT_METHOD.forLanguage(language);
  return codeInsightActionHandler != null && codeInsightActionHandler.isValidFor(editor, file);
}
 
Example 18
Source File: ShowParameterInfoAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull final PsiFile file) {
  final Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  return ShowParameterInfoHandler.getHandlers(project, language, file.getViewProvider().getBaseLanguage()) != null;
}
 
Example 19
Source File: ShowExpressionTypeAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull final PsiFile file) {
  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  return !ShowExpressionTypeHandler.getHandlers(project, language, file.getViewProvider().getBaseLanguage()).isEmpty();
}
 
Example 20
Source File: CompletionStyleUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static CommonCodeStyleSettings getCodeStyleSettings(@Nonnull InsertionContext context) {
  Language lang = PsiUtilCore.getLanguageAtOffset(context.getFile(), context.getTailOffset());
  return CodeStyle.getLanguageSettings(context.getFile(), lang);
}