Java Code Examples for com.intellij.openapi.command.WriteCommandAction#execute()

The following examples show how to use com.intellij.openapi.command.WriteCommandAction#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocGenerateAction.java    From CodeMaker with Apache License 2.0 5 votes vote down vote up
/**
 * Create doc for method.
 *
 * @param psiMethod the psi method
 * @param psiElementFactory the psi element factory
 */
private void createDocForMethod(PsiMethod psiMethod, PsiElementFactory psiElementFactory) {
    try {
        checkFilesAccess(psiMethod);
        PsiDocComment psiDocComment = psiMethod.getDocComment();
        //return if the method has comment
        if (psiDocComment != null) {
            return;
        }
        String interfaceName = CodeMakerUtil.findClassNameOfSuperMethod(psiMethod);
        if (interfaceName == null) {
            return;
        }
        String methodName = psiMethod.getName();
        Map<String, Object> map = Maps.newHashMap();
        map.put("interface", interfaceName);
        map.put("method", methodName);
        map.put("paramsType", generateMethodParamsType(psiMethod));
        String seeDoc = VelocityUtil.evaluate(seeDocTemplate, map);
        PsiDocComment seeDocComment = psiElementFactory.createDocCommentFromText(seeDoc);
        WriteCommandAction writeCommandAction = new DocWriteAction(psiMethod.getProject(), seeDocComment, psiMethod,
                psiMethod.getContainingFile());
        RunResult result = writeCommandAction.execute();
        if (result.hasException()) {
            LOGGER.error(result.getThrowable());
            Messages.showErrorDialog("CodeMaker plugin is not available, cause: " + result.getThrowable().getMessage(),
                    "CodeMaker plugin");
        }
    } catch (Exception e) {
        LOGGER.error("Create @see Doc failed", e);
    }
}
 
Example 2
Source File: Trees.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void replacePsiFileFromText(final Project project, Language language, final PsiFile psiFile, String text) {
	final PsiFile newPsiFile = createFile(project, language, text);
	if ( newPsiFile==null ) return;
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(@NotNull Result result) throws Throwable {
			psiFile.deleteChildRange(psiFile.getFirstChild(), psiFile.getLastChild());
			psiFile.addRange(newPsiFile.getFirstChild(), newPsiFile.getLastChild());
		}
	};
	setTextAction.execute();
}
 
Example 3
Source File: UniquifyRuleRefs.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
	PsiElement el = MyActionUtils.getSelectedPsiElement(e);
	if ( el==null ) return;

	final String ruleName = el.getText();

	final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
	if ( psiFile==null ) return;

	final Project project = e.getProject();

	Editor editor = e.getData(PlatformDataKeys.EDITOR);
	if ( editor==null ) return;
	final Document doc = editor.getDocument();

	String grammarText = psiFile.getText();
	ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
	Parser parser = results.parser;
	ParseTree tree = results.tree;

	// find all parser and lexer rule refs
	final List<TerminalNode> rrefNodes = RefactorUtils.getAllRuleRefNodes(parser, tree, ruleName);
	if ( rrefNodes==null ) return;

	// find rule def
	final TerminalNode ruleDefNameNode = RefactorUtils.getRuleDefNameNode(parser, tree, ruleName);
	if ( ruleDefNameNode==null ) return;

	// alter rule refs and dup rules
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			// do in a single action so undo works in one go
			dupRuleAndMakeRefsUnique(doc, ruleName, rrefNodes);
		}
	};
	setTextAction.execute();
}
 
Example 4
Source File: MyPsiUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void replacePsiFileFromText(final Project project, final PsiFile psiFile, String text) {
	final PsiFile newPsiFile = createFile(project, text);
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			psiFile.deleteChildRange(psiFile.getFirstChild(), psiFile.getLastChild());
			psiFile.addRange(newPsiFile.getFirstChild(), newPsiFile.getLastChild());
		}
	};
	setTextAction.execute();
}
 
Example 5
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void replaceText(final Project project, final Document doc,
                               final int start, final int stop, // inclusive
                               final String text)
{
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			doc.replaceString(start, stop+1, text);
		}
	};
	setTextAction.execute();
}
 
Example 6
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void insertText(final Project project, final Document doc,
                              final int where,
                              final String text)
{
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			doc.insertString(where, text);
		}
	};
	setTextAction.execute();
}
 
Example 7
Source File: GenerateCodeFromApiTableAction.java    From CodeMaker with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
        return;
    }
    DumbService dumbService = DumbService.getInstance(project);
    if (dumbService.isDumb()) {
        dumbService
            .showDumbModeNotification("CodeMaker plugin is not available during indexing");
        return;
    }
    PsiFile javaFile = e.getData(CommonDataKeys.PSI_FILE);
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (javaFile == null || editor == null) {
        return;
    }
    Transferable transferable = CopyPasteManager.getInstance().getContents();
    if (transferable == null) {
        return;
    }
    try {
        String table = (String) transferable.getTransferData(DataFlavor.stringFlavor);
        List<String> tableList = Splitter.onPattern("\n|\t|\r\n").splitToList(table);
        PsiElementFactory psiElementFactory = PsiElementFactory.SERVICE.getInstance(project);
        int mod = tableList.size() % 3;
        int end = tableList.size() - mod;

        for (int i = 0; i < end; i++) {
            String fieldName = tableList.get(i);
            String fieldType = tableList.get(++i);
            String fieldComment = tableList.get(++i);
            PsiField psiField = psiElementFactory.createField(fieldName,
                PsiType.getTypeByName(fieldType, project, GlobalSearchScope.allScope(project)));
            Map<String, Object> map = Maps.newHashMap();
            map.put("comment", fieldComment);
            String vm = "/**\n" + " * ${comment}\n" + " */";
            if (settings.getCodeTemplate("FieldComment.vm") != null) {
                vm = settings.getCodeTemplate("FieldComment.vm").getCodeTemplate();
            }
            String fieldDocComment = VelocityUtil.evaluate(vm, map);
            PsiDocComment docComment = psiElementFactory
                .createDocCommentFromText(fieldDocComment);
            psiField.addBefore(docComment, psiField.getFirstChild());
            WriteCommandAction writeCommandAction = new FieldWriteAction(project,
                CodeMakerUtil.getClasses(javaFile).get(0), psiField, javaFile);
            RunResult result = writeCommandAction.execute();
            if (result.hasException()) {
                LOGGER.error(result.getThrowable());
                Messages.showErrorDialog("CodeMaker plugin is not available, cause: "
                                         + result.getThrowable().getMessage(),
                    "CodeMaker plugin");
            }
        }
    } catch (Exception ex) {
        LOGGER.error(ex);
    }
}
 
Example 8
Source File: InlineRuleAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
	PsiElement el = MyActionUtils.getSelectedPsiElement(e);
	if ( el==null ) return;

	final String ruleName = el.getText();

	final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
	if ( psiFile==null ) return;

	final Project project = e.getProject();

	Editor editor = e.getData(PlatformDataKeys.EDITOR);
	if ( editor==null ) return;
	final Document doc = editor.getDocument();

	String grammarText = psiFile.getText();
	ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
	Parser parser = results.parser;
	ParseTree tree = results.tree;

	final CommonTokenStream tokens = (CommonTokenStream) parser.getTokenStream();

	// find all parser and lexer rule refs
	final List<TerminalNode> rrefNodes = RefactorUtils.getAllRuleRefNodes(parser, tree, ruleName);
	if ( rrefNodes==null ) return;

	// find rule def
	ParseTree ruleDefNameNode = RefactorUtils.getRuleDefNameNode(parser, tree, ruleName);
	if ( ruleDefNameNode==null ) return;

	// identify rhs of rule
	final ParserRuleContext ruleDefNode = (ParserRuleContext) ruleDefNameNode.getParent();
	String ruleText_ = RefactorUtils.getRuleText(tokens, ruleDefNode);

	// if rule has outermost alt, must add (...) around insertion
	// Look for ruleBlock, lexerRuleBlock
	if ( RefactorUtils.ruleHasMultipleOutermostAlts(parser, ruleDefNode) ) {
		ruleText_ = "("+ruleText_+")";
	}
	final String ruleText = ruleText_; // we ref from inner class; requires final

	// replace rule refs with rule text
	WriteCommandAction setTextAction = new WriteCommandAction(project) {
		@Override
		protected void run(final Result result) throws Throwable {
			// do in a single action so undo works in one go
			replaceRuleRefs(doc,tokens,ruleName,rrefNodes,ruleText);
		}
	};
	setTextAction.execute();
}