TextMateScope (@NotNull EditorEx editor) { final EditorHighlighter highlighter = editor.getHighlighter(); SelectionModel selection = editor.getSelectionModel(); final int offset = selection.hasSelection() ? selection.getSelectionStart() : editor.getCaretModel().getOffset(); final HighlighterIterator iterator = highlighter.createIterator(offset); TextMateScope result = null; if (offset != 0 || !iterator.atEnd()) { IElementType tokenType = iterator.getTokenType(); result = tokenType instanceof TextMateElementType ? ((TextMateElementType)tokenType).getScope() : null; } return result; }