@Override public void visitGenericCommand(BashCommand bashCommand) { if (bashCommand.isBashScriptCall()) { handleReference(bashCommand.getReference()); } }
public String findCacheKey(PsiElement element, PsiElement originalElement) { if (element instanceof BashCommand && ((BashCommand) element).isExternalCommand()) { return ((BashCommand) element).getReferencedCommandName(); } return null; }
@NotNull public String getType(@NotNull PsiElement element) { if (element instanceof BashFunctionDef || element instanceof BashFunctionDefName) { return "function"; } if (element instanceof BashCommand) { if (((BashCommand) element).isFunctionCall()) { return "function"; } if (((BashCommand) element).isBashScriptCall()) { return "bash script call"; } return "command"; } if (element instanceof BashVarDef || element instanceof BashVar) { return "variable"; } if (element instanceof BashHereDocMarker) { return "heredoc"; } if (element instanceof BashFile) { return "Bash file"; } return "unknown type"; }
public boolean isCommandLocal() { final PsiElement context = getContext(); if (context instanceof BashCommand) { final BashCommand parentCmd = (BashCommand) context; return !parentCmd.isPureAssignment() && !parentCmd.isVarDefCommand(); } return false; }
public BashCommandStub createStub(@NotNull BashCommand psi, StubElement parentStub) { String filename = null; String commandName = psi.getReferencedCommandName(); if (commandName != null) { filename = PathUtilRt.getFileName(commandName); } return new BashCommandStubImpl(parentStub, filename, BashElementTypes.SIMPLE_COMMAND_ELEMENT, psi.isInternalCommand(false), psi.isInternalCommand(true), psi.isGenericCommand()); }
@NotNull private PsiElement doCheckFunctionReference(boolean dumbMode) throws Exception { boolean oldDumb = DumbService.isDumb(myProject); DumbServiceImpl.getInstance(myProject).setDumb(dumbMode); try { PsiReference psiReference = configure(); Assert.assertTrue(psiReference.getElement() instanceof BashCommand); BashCommand commandElement = (BashCommand) psiReference.getElement(); Assert.assertTrue(psiReference.resolve() instanceof BashFunctionDef); Assert.assertTrue(commandElement.isFunctionCall()); Assert.assertFalse(commandElement.isVarDefCommand()); Assert.assertFalse(commandElement.isExternalCommand()); Assert.assertTrue(commandElement.getReference().isReferenceTo(psiReference.resolve())); return psiReference.resolve(); } finally { DumbServiceImpl.getInstance(myProject).setDumb(oldDumb); } }
if (bashCommand.isFunctionCall()) { cmdElement = bashCommand.commandElement(); attributesKey = BashSyntaxHighlighter.FUNCTION_CALL; } else if (bashCommand.isExternalCommand()) { cmdElement = bashCommand.commandElement(); attributesKey = BashSyntaxHighlighter.EXTERNAL_COMMAND; } else if (bashCommand.isInternalCommand()) { cmdElement = bashCommand.commandElement(); attributesKey = BashSyntaxHighlighter.INTERNAL_COMMAND;
@Override public void visitGenericCommand(BashCommand bashCommand) { if (!bashCommand.isFunctionCall()) { return; PsiReference reference = bashCommand.getReference(); if (reference == null) { return; List<BashPsiElement> callerParameters = bashCommand.parameters(); int callerParameterCount = callerParameters.size();
public boolean isLocalVarDef() { //check if the command is a local-var defining command, e.g. local final PsiElement context = getContext(); if (context instanceof BashCommand) { final BashCommand parentCmd = (BashCommand) context; String commandName = parentCmd.getReferencedCommandName(); //declared by "local" if (parentCmd.isVarDefCommand() && LanguageBuiltins.localVarDefCommands.contains(commandName)) { return true; } //declared by either delcare or typeset in a function block if (localVarDefCommands.contains(commandName) && BashPsiUtils.findNextVarDefFunctionDefScope(context) != null) { return true; } } return false; }
@Override String resourceNameForElement(PsiElement element) { if (element instanceof BashGenericCommand) { element = BashPsiUtils.findParent(element, BashCommand.class); } return (element instanceof BashCommand) ? ((BashCommand) element).getReferencedCommandName() : null; }
public String documentation(PsiElement element, PsiElement originalElement) { if (element instanceof DocumentationAwareElement) { return psiElementDocumentation((DocumentationAwareElement) element); } if (element instanceof BashGenericCommand) { element = BashPsiUtils.findParent(element, BashCommand.class); } if (element instanceof BashCommand) { BashCommand command = (BashCommand) element; PsiReference reference = command.getReference(); if (command.isFunctionCall() && reference != null) { PsiElement function = reference.resolve(); if (function instanceof DocumentationAwareElement) { return psiElementDocumentation((DocumentationAwareElement) function); } } } return null; }
public boolean isReadonly() { PsiElement context = getParent(); if (context instanceof BashCommand) { BashCommand command = (BashCommand) context; if (command.isInternalCommand() && LanguageBuiltins.readonlyVarDefCommands.contains(command.getReferencedCommandName())) { return true; } //check for declare -r or typeset -r if (isCommandWithParameter(command, commandsWithReadonlyOption, typeReadOnlyParams)) { return true; } } return false; }
private boolean isCommandWithParameter(BashCommand command, Set<String> validCommands, Set<String> validParams) { String commandName = command.getReferencedCommandName(); if (commandName != null && validCommands.contains(commandName)) { List<BashPsiElement> parameters = Lists.newArrayList(command.parameters()); for (BashPsiElement argValue : parameters) { for (String paramName : validParams) { if (BashCommandUtil.isParameterDefined(paramName, argValue.getText())) { return true; } } } } return false; }
boolean isValid(PsiElement element, PsiElement originalElement) { BashCommand command = commandElement(element); return command != null && command.isExternalCommand(); }
@NotNull @Override public TextRange getRangeInElement(@NotNull BashCommand cmd) { final PsiElement element = cmd.commandElement(); if (element == null) { return TextRange.from(0, cmd.getTextLength()); } return TextRange.from(element.getStartOffsetInParent(), element.getTextLength()); } }
@Override public BashCommand handleContentChange(@NotNull BashCommand cmd, @NotNull TextRange textRange, String newElementName) throws IncorrectOperationException { if (StringUtil.isEmpty(newElementName)) { throw new IncorrectOperationException("Can not handle empty names"); } PsiElement commandElement = cmd.commandElement(); if (commandElement == null) { throw new IncorrectOperationException("invalid command"); } BashGenericCommand replacement = BashPsiElementFactory.createCommand(cmd.getProject(), newElementName); BashPsiUtils.replaceElement(commandElement, replacement); return cmd; }
@Override public void visitInternalCommand(BashCommand bashCommand) { bashCommand.acceptChildren(wordVisitor); } });
@Override public BashCommand handleContentChange(@NotNull final BashCommand element, final String newContent) throws IncorrectOperationException { return handleContentChange(element, TextRange.create(0, element.getTextLength()), newContent); }
@Override public void visitVarDef(BashVarDef varDef) { if (varDef.isFunctionScopeLocal()) { PsiElement context = varDef.getContext(); if (context instanceof BashCommand) { final BashCommand parentCmd = (BashCommand) context; if (parentCmd.isVarDefCommand() && localVarDefCommands.contains(parentCmd.getReferencedCommandName())) { boolean isInFunction = false; PsiElement parent = BashPsiUtils.findEnclosingBlock(varDef); while (parent != null && !isInFunction) { isInFunction = parent instanceof BashFunctionDef; parent = BashPsiUtils.findEnclosingBlock(parent); } if (!isInFunction) { PsiElement problemHolder = varDef.getContext(); holder.registerProblem(problemHolder, "'local' must be used in a function", ProblemHighlightType.GENERIC_ERROR, new RemoveLocalQualifierQuickfix(varDef)); } } } } } };
@Override String resourceNameForElement(PsiElement element) { return commandElement(element).getReferencedCommandName(); }