Search in sources :

Example 1 with Macro

use of com.intellij.codeInsight.template.Macro in project intellij-community by JetBrains.

the class EditVariableDialog method createVariablesTable.

private JComponent createVariablesTable() {
    final String[] names = { CodeInsightBundle.message("templates.dialog.edit.variables.table.column.name"), CodeInsightBundle.message("templates.dialog.edit.variables.table.column.expression"), CodeInsightBundle.message("templates.dialog.edit.variables.table.column.default.value"), CodeInsightBundle.message("templates.dialog.edit.variables.table.column.skip.if.defined") };
    // Create a model of the data.
    TableModel dataModel = new VariablesModel(names);
    // Create the table
    myTable = new JBTable(dataModel);
    myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    myTable.setPreferredScrollableViewportSize(new Dimension(500, myTable.getRowHeight() * 8));
    myTable.getColumn(names[0]).setPreferredWidth(120);
    myTable.getColumn(names[1]).setPreferredWidth(200);
    myTable.getColumn(names[2]).setPreferredWidth(200);
    myTable.getColumn(names[3]).setPreferredWidth(100);
    if (myVariables.size() > 0) {
        myTable.getSelectionModel().setSelectionInterval(0, 0);
    }
    Predicate<Macro> isAcceptableInContext = macro -> myContextTypes.stream().anyMatch(macro::isAcceptableInContext);
    Stream<String> availableMacroNames = Arrays.stream(MacroFactory.getMacros()).filter(isAcceptableInContext).map(Macro::getPresentableName).sorted();
    Set<String> uniqueNames = availableMacroNames.collect(Collectors.toCollection(LinkedHashSet::new));
    ComboBox comboField = new ComboBox();
    uniqueNames.forEach(comboField::addItem);
    comboField.setEditable(true);
    DefaultCellEditor cellEditor = new DefaultCellEditor(comboField);
    cellEditor.setClickCountToStart(1);
    myTable.getColumn(names[1]).setCellEditor(cellEditor);
    myTable.setRowHeight(comboField.getPreferredSize().height);
    JTextField textField = new JTextField();
    /*textField.addMouseListener(
      new PopupHandler(){
        public void invokePopup(Component comp,int x,int y){
          showCellPopup((JTextField)comp,x,y);
        }
      }
    );*/
    cellEditor = new DefaultCellEditor(textField);
    cellEditor.setClickCountToStart(1);
    myTable.setDefaultEditor(String.class, cellEditor);
    final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myTable).disableAddAction().disableRemoveAction();
    return decorator.createPanel();
}
Also used : java.util(java.util) Document(com.intellij.openapi.editor.Document) ToolbarDecorator(com.intellij.ui.ToolbarDecorator) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) TableCellEditor(javax.swing.table.TableCellEditor) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) CommonBundle(com.intellij.CommonBundle) AbstractTableModel(javax.swing.table.AbstractTableModel) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType) TableModel(javax.swing.table.TableModel) ComboBox(com.intellij.openapi.ui.ComboBox) Predicate(java.util.function.Predicate) Editor(com.intellij.openapi.editor.Editor) Collectors(java.util.stream.Collectors) MacroFactory(com.intellij.codeInsight.template.macro.MacroFactory) java.awt(java.awt) CommandProcessor(com.intellij.openapi.command.CommandProcessor) HelpManager(com.intellij.openapi.help.HelpManager) JBTable(com.intellij.ui.table.JBTable) List(java.util.List) Stream(java.util.stream.Stream) Macro(com.intellij.codeInsight.template.Macro) EditableModel(com.intellij.util.ui.EditableModel) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) Macro(com.intellij.codeInsight.template.Macro) ComboBox(com.intellij.openapi.ui.ComboBox) JBTable(com.intellij.ui.table.JBTable) ToolbarDecorator(com.intellij.ui.ToolbarDecorator) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 2 with Macro

use of com.intellij.codeInsight.template.Macro in project intellij-community by JetBrains.

the class MacroParser method parseMacro.

//-----------------------------------------------------------------------------------
@SuppressWarnings({ "HardCodedStringLiteral" })
private static Expression parseMacro(Lexer lexer, String expression) {
    IElementType tokenType = lexer.getTokenType();
    String token = getString(lexer, expression);
    if (tokenType == MacroTokenType.STRING_LITERAL) {
        advance(lexer);
        return new ConstantNode(parseStringLiteral(token));
    }
    if (tokenType != MacroTokenType.IDENTIFIER) {
        LOG.info("Bad macro syntax: Not identifier: " + token);
        advance(lexer);
        return new ConstantNode("");
    }
    List<Macro> macros = MacroFactory.getMacros(token);
    if (macros.isEmpty()) {
        return parseVariable(lexer, expression);
    }
    advance(lexer);
    MacroCallNode macroCallNode = new MacroCallNode(macros.get(0));
    if (lexer.getTokenType() == null) {
        return macroCallNode;
    }
    if (lexer.getTokenType() != MacroTokenType.LPAREN) {
        return macroCallNode;
    }
    advance(lexer);
    parseParameters(macroCallNode, lexer, expression);
    if (lexer.getTokenType() != MacroTokenType.RPAREN) {
        LOG.info("Bad macro syntax: ) expected: " + expression);
    }
    advance(lexer);
    return macroCallNode;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Macro(com.intellij.codeInsight.template.Macro)

Aggregations

Macro (com.intellij.codeInsight.template.Macro)2 CommonBundle (com.intellij.CommonBundle)1 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 TemplateContextType (com.intellij.codeInsight.template.TemplateContextType)1 MacroFactory (com.intellij.codeInsight.template.macro.MacroFactory)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 HelpManager (com.intellij.openapi.help.HelpManager)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)1 IElementType (com.intellij.psi.tree.IElementType)1 ToolbarDecorator (com.intellij.ui.ToolbarDecorator)1 JBTable (com.intellij.ui.table.JBTable)1 EditableModel (com.intellij.util.ui.EditableModel)1 java.awt (java.awt)1 java.util (java.util)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1