private Action <ITextControl> ExecutePsiTransactionForExpression() { var throwExpressionModel = Error.ThrowExpression; var outerCatchClause = throwExpressionModel.FindOuterCatchClause(); string variableName; if (outerCatchClause.Node is ISpecificCatchClause) { variableName = ((ISpecificCatchClause)outerCatchClause.Node).ExceptionDeclaration.DeclaredName; } else { variableName = NameFactory.CatchVariableName(outerCatchClause.Node, outerCatchClause.CaughtException); } if (outerCatchClause.Node is ISpecificCatchClause) { outerCatchClause.AddCatchVariable(variableName); throwExpressionModel.AddInnerException(variableName); } else { throwExpressionModel.AddInnerException(variableName); outerCatchClause.AddCatchVariable(variableName); } return(null); }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var throwStatementModel = Error.ThrowStatement; var outerCatchClause = throwStatementModel.FindOuterCatchClause(); string variableName; if (outerCatchClause.Node is ISpecificCatchClause) { variableName = ((ISpecificCatchClause)outerCatchClause.Node).ExceptionDeclaration.DeclaredName; } else { variableName = NameFactory.CatchVariableName(outerCatchClause.Node, outerCatchClause.CaughtException); } if (outerCatchClause.Node is ISpecificCatchClause) { outerCatchClause.AddCatchVariable(variableName); throwStatementModel.AddInnerException(variableName); } else { throwStatementModel.AddInnerException(variableName); outerCatchClause.AddCatchVariable(variableName); } return(null); }
public void AddCatchVariable(string variableName) { if (Node is IGeneralCatchClause) { if (HasVariable) { return; } if (String.IsNullOrEmpty(variableName)) { variableName = NameFactory.CatchVariableName(Node, CaughtException); } var codeFactory = new CodeElementFactory(GetElementFactory()); var newCatch = codeFactory.CreateSpecificCatchClause(null, Node.Body, variableName); if (newCatch == null) { return; } Node.ReplaceBy(newCatch); Node = newCatch; Variable = new CatchVariableModel(AnalyzeUnit, newCatch.ExceptionDeclaration as ICatchVariableDeclaration); } else { if (HasVariable) { return; } if (String.IsNullOrEmpty(variableName)) { variableName = NameFactory.CatchVariableName(Node, CaughtException); } var specificNode = (ISpecificCatchClause)Node; var exceptionType = (IUserDeclaredTypeUsage)specificNode.ExceptionTypeUsage; var exceptionTypeName = exceptionType.TypeName.NameIdentifier.Name; var tempTry = GetElementFactory().CreateStatement("try {} catch($0 $1) {}", exceptionTypeName, variableName) as ITryStatement; if (tempTry == null) { return; } var tempCatch = tempTry.Catches[0] as ISpecificCatchClause; if (tempCatch == null) { return; } var resultVariable = specificNode.SetExceptionDeclaration(tempCatch.ExceptionDeclaration); Variable = new CatchVariableModel(AnalyzeUnit, resultVariable); } }
/// <summary>Adds a catch clause to the try statement. </summary> /// <param name="exceptionType">The exception type in the added catch clause. </param> public void AddCatchClause(IDeclaredType exceptionType) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var variableName = NameFactory.CatchVariableName(Node, exceptionType); var catchClauseNode = codeElementFactory.CreateSpecificCatchClause(exceptionType, null, variableName); Node.AddCatchClause(catchClauseNode); }
/// <summary>Creates a try-catch block around this block. </summary> /// <param name="exceptionType">The exception type to catch. </param> /// <returns><c>true</c> if the try-catch block could be created; otherwise, <c>false</c>. </returns> public bool SurroundWithTryBlock(IDeclaredType exceptionType) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var exceptionVariableName = NameFactory.CatchVariableName(Node, exceptionType); var tryStatement = codeElementFactory.CreateTryStatement(exceptionType, exceptionVariableName, Node); var block = codeElementFactory.CreateBlock(Node); tryStatement.SetTry(block); Node.ReplaceBy(tryStatement); return(true); }
/// <summary>Creates a try-catch block around this block. </summary> /// <param name="exceptionType">The exception type to catch. </param> /// <returns><c>true</c> if the try-catch block could be created; otherwise, <c>false</c>. </returns> public bool SurroundWithTryBlock(IDeclaredType exceptionType) { var containingStatement = Node.GetContainingStatement(); if (containingStatement != null && containingStatement.LastChild != null) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var exceptionVariableName = NameFactory.CatchVariableName(Node, exceptionType); var tryStatement = codeElementFactory.CreateTryStatement(exceptionType, exceptionVariableName, Node); var spaces = GetElementFactory().CreateWhitespaces(Environment.NewLine); LowLevelModificationUtil.AddChildAfter(containingStatement.LastChild, spaces[0]); var block = codeElementFactory.CreateBlock(containingStatement); tryStatement.SetTry(block); containingStatement.ReplaceBy(tryStatement); return(true); } return(false); }