use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class FormatterUtil method spacesOnly.
private static boolean spacesOnly(@Nullable TreeElement node) {
if (node == null)
return false;
if (isWhitespaceOrEmpty(node))
return true;
PsiElement psi = node.getPsi();
if (psi == null) {
return false;
}
Language language = psi.getLanguage();
return WhiteSpaceFormattingStrategyFactory.getStrategy(language).containsWhitespacesOnly(node);
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class TargetElementUtil method getReferenceOrReferencedElement.
@Nullable
private PsiElement getReferenceOrReferencedElement(PsiFile file, Editor editor, int flags, int offset) {
PsiElement result = doGetReferenceOrReferencedElement(file, editor, flags, offset);
PsiElement languageElement = file.findElementAt(offset);
Language language = languageElement != null ? languageElement.getLanguage() : file.getLanguage();
TargetElementEvaluatorEx2 evaluator = getElementEvaluatorsEx2(language);
if (evaluator != null) {
result = evaluator.adjustReferenceOrReferencedElement(file, editor, offset, flags, result);
}
return result;
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class CompletionUtil method getCompletionDataByElement.
@Nullable
public static CompletionData getCompletionDataByElement(@Nullable final PsiElement position, @NotNull PsiFile originalFile) {
if (position == null)
return null;
PsiElement parent = position.getParent();
Language language = parent == null ? position.getLanguage() : parent.getLanguage();
final FileType fileType = language.getAssociatedFileType();
if (fileType != null) {
final CompletionData mainData = getCompletionDataByFileType(fileType);
if (mainData != null) {
return mainData;
}
}
final CompletionData mainData = getCompletionDataByFileType(originalFile.getFileType());
return mainData != null ? mainData : ourGenericCompletionData;
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class EnterAfterUnmatchedBraceHandler method getUnmatchedLBracesNumberBefore.
/**
* Calculates number of unmatched left braces before the given offset.
*
* @param editor target editor
* @param offset target offset
* @param fileType target file type
* @return number of unmatched braces before the given offset;
* negative value if it's not possible to perform the calculation or if there are no unmatched left braces before
* the given offset
*/
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType) {
if (offset == 0) {
return -1;
}
CharSequence chars = editor.getDocument().getCharsSequence();
if (chars.charAt(offset - 1) != '{') {
return -1;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
if (!braceMatcher.isLBraceToken(iterator, chars, fileType) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
return -1;
}
Language language = iterator.getTokenType().getLanguage();
iterator = highlighter.createIterator(0);
int lBracesBeforeOffset = 0;
int lBracesAfterOffset = 0;
int rBracesBeforeOffset = 0;
int rBracesAfterOffset = 0;
for (; !iterator.atEnd(); iterator.advance()) {
IElementType tokenType = iterator.getTokenType();
if (!tokenType.getLanguage().equals(language) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
continue;
}
boolean beforeOffset = iterator.getStart() < offset;
if (braceMatcher.isLBraceToken(iterator, chars, fileType)) {
if (beforeOffset) {
lBracesBeforeOffset++;
} else {
lBracesAfterOffset++;
}
} else if (braceMatcher.isRBraceToken(iterator, chars, fileType)) {
if (beforeOffset) {
rBracesBeforeOffset++;
} else {
rBracesAfterOffset++;
}
}
}
return lBracesBeforeOffset - rBracesBeforeOffset - (rBracesAfterOffset - lBracesAfterOffset);
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class CommentByLineCommentHandler method findCommenter.
@Nullable
private static Commenter findCommenter(Editor editor, PsiFile file, final int line) {
final FileType fileType = file.getFileType();
if (fileType instanceof AbstractFileType) {
return ((AbstractFileType) fileType).getCommenter();
}
Document document = editor.getDocument();
int lineStartOffset = document.getLineStartOffset(line);
int lineEndOffset = document.getLineEndOffset(line) - 1;
final CharSequence charSequence = document.getCharsSequence();
lineStartOffset = Math.max(0, CharArrayUtil.shiftForward(charSequence, lineStartOffset, " \t"));
lineEndOffset = Math.max(0, CharArrayUtil.shiftBackward(charSequence, lineEndOffset < 0 ? 0 : lineEndOffset, " \t"));
final Language lineStartLanguage = PsiUtilCore.getLanguageAtOffset(file, lineStartOffset);
final Language lineEndLanguage = PsiUtilCore.getLanguageAtOffset(file, lineEndOffset);
return CommentByBlockCommentHandler.getCommenter(file, editor, lineStartLanguage, lineEndLanguage);
}
Aggregations