protected OuterLanguageElementImpl createOuterLanguageElement( final Lexer lexer, final CharTable table, final IElementType outerElementType) { final CharSequence buffer = lexer.getBufferSequence(); final int tokenStart = lexer.getTokenStart(); if (tokenStart < 0 || tokenStart > buffer.length()) { LOG.error("Invalid start: " + tokenStart + "; " + lexer); } final int tokenEnd = lexer.getTokenEnd(); if (tokenEnd < 0 || tokenEnd > buffer.length()) { LOG.error("Invalid end: " + tokenEnd + "; " + lexer); } return new OuterLanguageElementImpl( outerElementType, table.intern(buffer, tokenStart, tokenEnd)); }
private boolean paintPlaceholderText(Graphics2D g) { CharSequence hintText = myEditor.getPlaceholder(); EditorComponentImpl editorComponent = myEditor.getContentComponent(); if (myDocument.getTextLength() > 0 || hintText == null || hintText.length() == 0 || KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == editorComponent && !myEditor.getShowPlaceholderWhenFocused()) { return false; } hintText = SwingUtilities.layoutCompoundLabel( g.getFontMetrics(), hintText.toString(), null, 0, 0, 0, 0, editorComponent.getBounds(), new Rectangle(), new Rectangle(), 0); g.setColor(myEditor.getFoldingModel().getPlaceholderAttributes().getForegroundColor()); g.setFont(myEditor.getColorsScheme().getFont(EditorFontType.PLAIN)); g.drawString(hintText.toString(), 0, myView.getAscent()); return true; }
private static void assertBeforeCommit( Document document, PsiFile file, TextBlock textBlock, CharSequence chars, String oldPsiText, FileElement myTreeElementBeingReparsedSoItWontBeCollected) { int startOffset = textBlock.getStartOffset(); int psiEndOffset = textBlock.getPsiEndOffset(); if (oldPsiText != null) { @NonNls String msg = "PSI/document inconsistency before reparse: "; if (startOffset >= oldPsiText.length()) { msg += "startOffset=" + oldPsiText + " while text length is " + oldPsiText.length() + "; "; startOffset = oldPsiText.length(); } String psiPrefix = oldPsiText.substring(0, startOffset); String docPrefix = chars.subSequence(0, startOffset).toString(); String psiSuffix = oldPsiText.substring(psiEndOffset); String docSuffix = chars.subSequence(textBlock.getTextEndOffset(), chars.length()).toString(); if (!psiPrefix.equals(docPrefix) || !psiSuffix.equals(docSuffix)) { if (!psiPrefix.equals(docPrefix)) { msg = msg + "psiPrefix=" + psiPrefix + "; docPrefix=" + docPrefix + ";"; } if (!psiSuffix.equals(docSuffix)) { msg = msg + "psiSuffix=" + psiSuffix + "; docSuffix=" + docSuffix + ";"; } throw new AssertionError(msg); } } else if (document.getTextLength() - textBlock.getTextEndOffset() != myTreeElementBeingReparsedSoItWontBeCollected.getTextLength() - psiEndOffset) { throw new AssertionError("PSI/document inconsistency before reparse: file=" + file); } }
private void end(StringBuilder ans, Pretty pretty, String indent) { if (endEncloser == null) return; if (!pretty.pretty) ans.append(endEncloser); else { Utilities.assureEndsWithNewLine(ans); ans.append(indent + endEncloser); if (pretty.comments && endComment.length() > 0) ans.append("#" + endComment); Utilities.assureEndsWithNewLine(ans); } }
private void start(StringBuilder ans, Pretty pretty, String indent) { if (startEncloser == null) return; if (!pretty.pretty) { ans.append(startEncloser); } else { String comment; if (pretty.comments && startComment.length() > 0) comment = " #" + startComment; else comment = ""; ans.append(indent + startEncloser + comment); Utilities.assureEndsWithNewLine(ans); } }
private CharSequence createTemplateText(CharSequence buf, Lexer lexer) { StringBuilder result = new StringBuilder(buf.length()); lexer.start(buf); while (lexer.getTokenType() != null) { if (lexer.getTokenType() == myTemplateElementType) { appendCurrentTemplateToken(result, buf, lexer); } lexer.advance(); } return result; }
public static boolean checkConsistency(PsiFile psiFile, Document document) { // todo hack if (psiFile.getVirtualFile() == null) return true; CharSequence editorText = document.getCharsSequence(); int documentLength = document.getTextLength(); if (psiFile.textMatches(editorText)) { LOG.assertTrue(psiFile.getTextLength() == documentLength); return true; } char[] fileText = psiFile.textToCharArray(); @SuppressWarnings({"NonConstantStringShouldBeStringBuffer"}) @NonNls String error = "File '" + psiFile.getName() + "' text mismatch after reparse. " + "File length=" + fileText.length + "; Doc length=" + documentLength + "\n"; int i = 0; for (; i < documentLength; i++) { if (i >= fileText.length) { error += "editorText.length > psiText.length i=" + i + "\n"; break; } if (i >= editorText.length()) { error += "editorText.length > psiText.length i=" + i + "\n"; break; } if (editorText.charAt(i) != fileText[i]) { error += "first unequal char i=" + i + "\n"; break; } } // error += "*********************************************" + "\n"; // if (i <= 500){ // error += "Equal part:" + editorText.subSequence(0, i) + "\n"; // } // else{ // error += "Equal part start:\n" + editorText.subSequence(0, 200) + "\n"; // error += "................................................" + "\n"; // error += "................................................" + "\n"; // error += "................................................" + "\n"; // error += "Equal part end:\n" + editorText.subSequence(i - 200, i) + "\n"; // } error += "*********************************************" + "\n"; error += "Editor Text tail:(" + (documentLength - i) + ")\n"; // + editorText.subSequence(i, Math.min(i + 300, documentLength)) + "\n"; error += "*********************************************" + "\n"; error += "Psi Text tail:(" + (fileText.length - i) + ")\n"; error += "*********************************************" + "\n"; if (document instanceof DocumentWindow) { error += "doc: '" + document.getText() + "'\n"; error += "psi: '" + psiFile.getText() + "'\n"; error += "ast: '" + psiFile.getNode().getText() + "'\n"; error += psiFile.getLanguage() + "\n"; PsiElement context = InjectedLanguageManager.getInstance(psiFile.getProject()).getInjectionHost(psiFile); if (context != null) { error += "context: " + context + "; text: '" + context.getText() + "'\n"; error += "context file: " + context.getContainingFile() + "\n"; } error += "document window ranges: " + Arrays.asList(((DocumentWindow) document).getHostRanges()) + "\n"; } LOG.error(error); // document.replaceString(0, documentLength, psiFile.getText()); return false; }
/** * @param hideByAnyKey * @param x <code>x</code> coordinate in layered pane coordinate system. * @param y <code>y</code> coordinate in layered pane coordinate system. */ @Nullable public static LightweightHint showEditorFragmentHintAt( Editor editor, TextRange range, int x, int y, boolean showUpward, boolean showFolding, boolean hideByAnyKey) { if (ApplicationManager.getApplication().isUnitTestMode()) return null; Document document = editor.getDocument(); int startOffset = range.getStartOffset(); int startLine = document.getLineNumber(startOffset); CharSequence text = document.getCharsSequence(); // There is a possible case that we have a situation like below: // line 1 // line 2 <fragment start> // line 3<fragment end> // We don't want to include 'line 2' to the target fragment then. boolean incrementLine = false; for (int offset = startOffset, max = Math.min(range.getEndOffset(), text.length()); offset < max; offset++) { char c = text.charAt(offset); incrementLine = StringUtil.isWhiteSpace(c); if (!incrementLine || c == '\n') { break; } } if (incrementLine) { startLine++; } int endLine = Math.min(document.getLineNumber(range.getEndOffset()) + 1, document.getLineCount() - 1); // if (editor.logicalPositionToXY(new LogicalPosition(startLine, 0)).y >= // editor.logicalPositionToXY(new LogicalPosition(endLine, 0)).y) return null; if (startLine >= endLine) return null; EditorFragmentComponent fragmentComponent = createEditorFragmentComponent(editor, startLine, endLine, showFolding, true); if (showUpward) { y -= fragmentComponent.getPreferredSize().height + 10; y = Math.max(0, y); } final JComponent c = editor.getComponent(); x = SwingUtilities.convertPoint(c, new Point(-3, 0), UIUtil.getRootPane(c)).x; // IDEA-68016 Point p = new Point(x, y); LightweightHint hint = new MyComponentHint(fragmentComponent); HintManagerImpl.getInstanceImpl() .showEditorHint( hint, editor, p, (hideByAnyKey ? HintManager.HIDE_BY_ANY_KEY : 0) | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_MOUSEOVER, 0, false, new HintHint(editor, p)); return hint; }