private static Editor createEditor() { EditorFactory editorFactory = EditorFactory.getInstance(); Document document = editorFactory.createDocument(""); EditorEx editor = (EditorEx) editorFactory.createEditor(document); reinitSettings(editor); return editor; }
public LanguageConsoleImpl( Project project, String title, LightVirtualFile lightFile, boolean initComponents) { myProject = project; myTitle = title; myVirtualFile = lightFile; EditorFactory editorFactory = EditorFactory.getInstance(); myHistoryFile = new LightVirtualFile(getTitle() + ".history.txt", FileTypes.PLAIN_TEXT, ""); myEditorDocument = FileDocumentManager.getInstance().getDocument(lightFile); reparsePsiFile(); assert myEditorDocument != null; myConsoleEditor = (EditorEx) editorFactory.createEditor(myEditorDocument, myProject); myConsoleEditor.addFocusListener(myFocusListener); myCurrentEditor = myConsoleEditor; myHistoryViewer = (EditorEx) editorFactory.createViewer( ((EditorFactoryImpl) editorFactory).createDocument(true), myProject); myUpdateQueue = new MergingUpdateQueue("ConsoleUpdateQueue", 300, true, null); Disposer.register(this, myUpdateQueue); // action shortcuts are not yet registered ApplicationManager.getApplication() .invokeLater( new Runnable() { @Override public void run() { installEditorFactoryListener(); } }); if (initComponents) { initComponents(); } }
public ImplementationViewComponent(PsiElement[] elements, final int index) { super(new BorderLayout()); final Project project = elements.length > 0 ? elements[0].getProject() : null; EditorFactory factory = EditorFactory.getInstance(); Document doc = factory.createDocument(""); doc.setReadOnly(true); myEditor = factory.createEditor(doc, project); ((EditorEx) myEditor).setBackgroundColor(EditorFragmentComponent.getBackgroundColor(myEditor)); final EditorSettings settings = myEditor.getSettings(); settings.setAdditionalLinesCount(1); settings.setAdditionalColumnsCount(1); settings.setLineMarkerAreaShown(false); settings.setIndentGuidesShown(false); settings.setLineNumbersShown(false); settings.setFoldingOutlineShown(false); myBinarySwitch = new CardLayout(); myViewingPanel = new JPanel(myBinarySwitch); myEditor.setBorder(null); ((EditorEx) myEditor).getScrollPane().setViewportBorder(JBScrollPane.createIndentBorder()); myViewingPanel.add(myEditor.getComponent(), TEXT_PAGE_KEY); myBinaryPanel = new JPanel(new BorderLayout()); myViewingPanel.add(myBinaryPanel, BINARY_PAGE_KEY); add(myViewingPanel, BorderLayout.CENTER); myToolbar = createToolbar(); myLocationLabel = new JLabel(); myCountLabel = new JLabel(); final JPanel header = new JPanel(new BorderLayout(2, 0)); header.setBorder( BorderFactory.createCompoundBorder( IdeBorderFactory.createBorder(SideBorder.BOTTOM), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5))); final JPanel toolbarPanel = new JPanel(new GridBagLayout()); final GridBagConstraints gc = new GridBagConstraints( GridBagConstraints.RELATIVE, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0); toolbarPanel.add(myToolbar.getComponent(), gc); setPreferredSize(new Dimension(600, 400)); update( elements, new PairFunction<PsiElement[], List<FileDescriptor>, Boolean>() { @Override public Boolean fun( final PsiElement[] psiElements, final List<FileDescriptor> fileDescriptors) { if (psiElements.length == 0) return false; myElements = psiElements; myIndex = index < myElements.length ? index : 0; PsiFile psiFile = getContainingFile(myElements[myIndex]); VirtualFile virtualFile = psiFile.getVirtualFile(); EditorHighlighter highlighter; if (virtualFile != null) highlighter = HighlighterFactory.createHighlighter(project, virtualFile); else { String fileName = psiFile.getName(); // some artificial psi file, lets do best we can highlighter = HighlighterFactory.createHighlighter(project, fileName); } ((EditorEx) myEditor).setHighlighter(highlighter); gc.fill = GridBagConstraints.HORIZONTAL; gc.weightx = 1; if (myElements.length > 1) { myFileChooser = new ComboBox( fileDescriptors.toArray(new FileDescriptor[fileDescriptors.size()]), 250); updateRenderer(project); myFileChooser.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int index = myFileChooser.getSelectedIndex(); if (myIndex != index) { myIndex = index; updateControls(); } } }); myLabel = new JLabel(); myLabel.setVisible(false); toolbarPanel.add(myFileChooser, gc); } else { myFileChooser = new ComboBox(); myFileChooser.setVisible(false); myCountLabel.setVisible(false); myLabel = new JLabel(); VirtualFile file = psiFile.getVirtualFile(); if (file != null) { myLabel.setIcon(getIconForFile(psiFile)); myLabel.setForeground( FileStatusManager.getInstance(project).getStatus(file).getColor()); myLabel.setText(file.getPresentableName()); myLabel.setBorder( new CompoundBorder( IdeBorderFactory.createRoundedBorder(), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5))); } toolbarPanel.add(myLabel, gc); } gc.fill = GridBagConstraints.NONE; gc.weightx = 0; toolbarPanel.add(myCountLabel, gc); header.add(toolbarPanel, BorderLayout.CENTER); header.add(myLocationLabel, BorderLayout.EAST); add(header, BorderLayout.NORTH); updateControls(); return true; } }); }
/** * Inspects all lines of the given document and wraps all of them that exceed {@link * CodeStyleSettings#RIGHT_MARGIN right margin}. * * <p>I.e. the algorithm is to do the following for every line: * * <p> * * <pre> * <ol> * <li> * Check if the line exceeds {@link CodeStyleSettings#RIGHT_MARGIN right margin}. Go to the next line in the case of * negative answer; * </li> * <li>Determine line wrap position; </li> * <li> * Perform 'smart wrap', i.e. not only wrap the line but insert additional characters over than line feed if necessary. * For example consider that we wrap a single-line comment - we need to insert comment symbols on a start of the wrapped * part as well. Generally, we get the same behavior as during pressing 'Enter' at wrap position during editing document; * </li> * </ol> * </pre> * * @param file file that holds parsed document tree * @param document target document * @param startOffset start offset of the first line to check for wrapping (inclusive) * @param endOffset end offset of the first line to check for wrapping (exclusive) */ private void wrapLongLinesIfNecessary( @NotNull final PsiFile file, @Nullable final Document document, final int startOffset, final int endOffset) { if (!mySettings.getCommonSettings(file.getLanguage()).WRAP_LONG_LINES || PostprocessReformattingAspect.getInstance(file.getProject()) .isViewProviderLocked(file.getViewProvider()) || document == null) { return; } final VirtualFile vFile = FileDocumentManager.getInstance().getFile(document); if ((vFile == null || vFile instanceof LightVirtualFile) && !ApplicationManager.getApplication().isUnitTestMode()) { // we assume that control flow reaches this place when the document is backed by a "virtual" // file so any changes made by // a formatter affect only PSI and it is out of sync with a document text return; } Editor editor = PsiUtilBase.findEditor(file); EditorFactory editorFactory = null; if (editor == null) { if (!ApplicationManager.getApplication().isDispatchThread()) { return; } editorFactory = EditorFactory.getInstance(); editor = editorFactory.createEditor(document, file.getProject()); } try { final Editor editorToUse = editor; ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { final CaretModel caretModel = editorToUse.getCaretModel(); final int caretOffset = caretModel.getOffset(); final RangeMarker caretMarker = editorToUse.getDocument().createRangeMarker(caretOffset, caretOffset); doWrapLongLinesIfNecessary( editorToUse, file.getProject(), editorToUse.getDocument(), startOffset, endOffset); if (caretMarker.isValid() && caretModel.getOffset() != caretMarker.getStartOffset()) { caretModel.moveToOffset(caretMarker.getStartOffset()); } } }); } finally { PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject()); if (documentManager.isUncommited(document)) documentManager.commitDocument(document); if (editorFactory != null) { editorFactory.releaseEditor(editor); } } }