Beispiel #1
0
 private static void FormatCode(IDeclarationStatement declarationStatement)
 {
     var languageService = declarationStatement.Language.LanguageService();
     if (languageService == null)
         return;
     languageService.CodeFormatter.Format(
         declarationStatement,
         CodeFormatProfile.GENERATOR);
 }
    /// <summary>
    /// Gets the state of the expression null reference.
    /// </summary>
    /// <param name="treeNode">The element.</param>
    /// <param name="declaredElement">The declared element.</param>
    /// <param name="statement">The statement.</param>
    /// <returns>Returns the expression null reference state.</returns>
    private CodeAnnotationAttribute GetNullReferenceState(ITreeNode treeNode, IDeclaredElement declaredElement, IDeclarationStatement statement)
    {
      var project = treeNode.GetProject();
      if (project == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var projectFile = project.ProjectFile;
      if (projectFile == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var functionDeclaration = treeNode.GetContainingNode<ICSharpFunctionDeclaration>(true);
      if (functionDeclaration == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var builder = new CSharpControlFlowBuilder();
      var graf = builder.GraphFromNode(functionDeclaration, null, true) as ICSharpControlFlowGraph;
      if (graf == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var inspect = graf.Inspect(ValueAnalysisMode.OPTIMISTIC);

      var position = this.FindPosition(graf.BodyElement.Children, statement);
      if (position == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      position = this.FindFollowing(position);
      if (position == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var result = inspect.GetVariableStateAt(position, declaredElement);
      switch (result)
      {
        case CSharpControlFlowNullReferenceState.NOT_NULL:
          return CodeAnnotationAttribute.NotNull;

        case CSharpControlFlowNullReferenceState.NULL:
          return CodeAnnotationAttribute.CanBeNull;

        case CSharpControlFlowNullReferenceState.MAY_BE_NULL:
          return CodeAnnotationAttribute.CanBeNull;
      }

      return CodeAnnotationAttribute.Undefined;

      /*
      var block = treeNode.GetContainingNode<IBlock>(true);
      if (block == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var project = treeNode.GetProject();
      if (project == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var projectFile = project.ProjectFile;
      if (projectFile == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var factory = CSharpElementFactory.GetInstance(treeNode.GetPsiModule());
      var statement = factory.CreateStatement("if(" + name + " == null) { }");

      var ifStatement = block.AddStatementAfter(statement, (ICSharpStatement)anchorStatement) as IIfStatement;
      if (ifStatement == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var equalityExpression = ifStatement.Condition as IEqualityExpression;
      if (equalityExpression == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var referenceExpression = equalityExpression.LeftOperand as IReferenceExpression;
      if (referenceExpression == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var functionDeclaration = ifStatement.GetContainingNode<ICSharpFunctionDeclaration>(true);
      if (functionDeclaration == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var graf = CSharpControlFlowBuilder.Build(functionDeclaration);
      if (graf == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var inspect = graf.Inspect(HighlightingSettingsManager.Instance.GetValueAnalysisMode(projectFile));
      if (inspect == null)
      {
        return CodeAnnotationAttribute.Undefined;
      }

      var position = graf.AllElements.FirstOrDefault(e => e.SourceElement == treeNode);

      var result = inspect.GetVariableStateAt(position, declaredElement);
        
      result = inspect.GetExpressionNullReferenceState(referenceExpression, false);
      switch (result)
      {
        case CSharpControlFlowNullReferenceState.NOT_NULL:
          return CodeAnnotationAttribute.NotNull;

        case CSharpControlFlowNullReferenceState.NULL:
          return CodeAnnotationAttribute.CanBeNull;

        case CSharpControlFlowNullReferenceState.MAY_BE_NULL:
          return CodeAnnotationAttribute.CanBeNull;
      }

      return CodeAnnotationAttribute.Undefined;
      */
    }
        /// <summary>
        /// Gets the state of the expression null reference.
        /// </summary>
        /// <param name="treeNode">The element.</param>
        /// <param name="declaredElement">The declared element.</param>
        /// <param name="statement">The statement.</param>
        /// <returns>Returns the expression null reference state.</returns>
        private CodeAnnotationAttribute GetNullReferenceState(ITreeNode treeNode, IDeclaredElement declaredElement, IDeclarationStatement statement)
        {
            var project = treeNode.GetProject();

            if (project == null)
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            var projectFile = project.ProjectFile;

            if (projectFile == null)
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            var functionDeclaration = treeNode.GetContainingNode <ICSharpFunctionDeclaration>(true);

            if (functionDeclaration == null)
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            var builder = new CSharpControlFlowBuilder();
            var graph   = builder.GraphFromNode(functionDeclaration, null, true) as ICSharpControlFlowGraph;

            if (graph == null)
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            var graphInspector = CSharpControlFlowGraphInspector.Inspect(graph, ValueAnalysisMode.OPTIMISTIC);

            var position = this.FindPosition(graph.BodyElement.Children, statement);

            if (position == null)
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            position = this.FindFollowing(position);
            if (position == null)
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            var result = graphInspector.GetVariableStateAt(position, declaredElement);

            switch (result)
            {
            case CSharpControlFlowNullReferenceState.NOT_NULL:
                return(CodeAnnotationAttribute.NotNull);

            case CSharpControlFlowNullReferenceState.NULL:
                return(CodeAnnotationAttribute.CanBeNull);

            case CSharpControlFlowNullReferenceState.MAY_BE_NULL:
                return(CodeAnnotationAttribute.CanBeNull);
            }

            return(CodeAnnotationAttribute.Undefined);

            /*
             * var block = treeNode.GetContainingNode<IBlock>(true);
             * if (block == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var project = treeNode.GetProject();
             * if (project == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var projectFile = project.ProjectFile;
             * if (projectFile == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var factory = CSharpElementFactory.GetInstance(treeNode.GetPsiModule());
             * var statement = factory.CreateStatement("if(" + name + " == null) { }");
             *
             * var ifStatement = block.AddStatementAfter(statement, (ICSharpStatement)anchorStatement) as IIfStatement;
             * if (ifStatement == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var equalityExpression = ifStatement.Condition as IEqualityExpression;
             * if (equalityExpression == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var referenceExpression = equalityExpression.LeftOperand as IReferenceExpression;
             * if (referenceExpression == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var functionDeclaration = ifStatement.GetContainingNode<ICSharpFunctionDeclaration>(true);
             * if (functionDeclaration == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var graf = CSharpControlFlowBuilder.Build(functionDeclaration);
             * if (graf == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var inspect = graf.Inspect(HighlightingSettingsManager.Instance.GetValueAnalysisMode(projectFile));
             * if (inspect == null)
             * {
             * return CodeAnnotationAttribute.Undefined;
             * }
             *
             * var position = graf.AllElements.FirstOrDefault(e => e.SourceElement == treeNode);
             *
             * var result = inspect.GetVariableStateAt(position, declaredElement);
             *
             * result = inspect.GetExpressionNullReferenceState(referenceExpression, false);
             * switch (result)
             * {
             * case CSharpControlFlowNullReferenceState.NOT_NULL:
             *  return CodeAnnotationAttribute.NotNull;
             *
             * case CSharpControlFlowNullReferenceState.NULL:
             *  return CodeAnnotationAttribute.CanBeNull;
             *
             * case CSharpControlFlowNullReferenceState.MAY_BE_NULL:
             *  return CodeAnnotationAttribute.CanBeNull;
             * }
             *
             * return CodeAnnotationAttribute.Undefined;
             */
        }
    /// <summary>
    /// Gets the state of the expression null reference.
    /// </summary>
    /// <param name="treeNode">The element.</param>
    /// <param name="declaredElement">The declared element.</param>
    /// <param name="anchor">The anchor.</param>
    /// <returns>Returns the expression null reference state.</returns>
    public CodeAnnotationAttribute GetExpressionNullReferenceState(ITreeNode treeNode, IDeclaredElement declaredElement, IDeclarationStatement anchor)
    {
      return this.GetNullReferenceState(treeNode, declaredElement, anchor);

      /*
      var state = CodeAnnotationAttribute.Undefined;

      const string CookieName = "CodeAnnotations";

      var solution = treeNode.GetSolution();

      var cookie = solution.CreateTransactionCookie(DefaultAction.Rollback, CookieName);
      try
      {
        var psiServices = solution.GetPsiServices();

        Shell.Instance.GetComponent<UITaskExecutor>().SingleThreaded.ExecuteTask(CookieName, TaskCancelable.Yes, delegate(IProgressIndicator progress1)
        {
          progress1.TaskName = CookieName;

          psiServices.PsiManager.DoTransaction(() => state = this.GetNullReferenceState(treeNode, name, anchorStatement), CookieName);

          cookie.Rollback();
        });
      }
      finally
      {
        if (cookie != null)
        {
          cookie.Dispose();
        }
      }

      return state;
      */
    }
        /// <summary>
        /// Gets the state of the expression null reference.
        /// </summary>
        /// <param name="treeNode">The element.</param>
        /// <param name="declaredElement">The declared element.</param>
        /// <param name="anchor">The anchor.</param>
        /// <returns>Returns the expression null reference state.</returns>
        public CodeAnnotationAttribute GetExpressionNullReferenceState(ITreeNode treeNode, IDeclaredElement declaredElement, IDeclarationStatement anchor)
        {
            return(this.GetNullReferenceState(treeNode, declaredElement, anchor));

            /*
             * var state = CodeAnnotationAttribute.Undefined;
             *
             * const string CookieName = "CodeAnnotations";
             *
             * var solution = treeNode.GetSolution();
             *
             * var cookie = solution.CreateTransactionCookie(DefaultAction.Rollback, CookieName);
             * try
             * {
             * var psiServices = solution.GetPsiServices();
             *
             * Shell.Instance.GetComponent<UITaskExecutor>().SingleThreaded.ExecuteTask(CookieName, TaskCancelable.Yes, delegate(IProgressIndicator progress1)
             * {
             *  progress1.TaskName = CookieName;
             *
             *  psiServices.PsiManager.DoTransaction(() => state = this.GetNullReferenceState(treeNode, name, anchorStatement), CookieName);
             *
             *  cookie.Rollback();
             * });
             * }
             * finally
             * {
             * if (cookie != null)
             * {
             *  cookie.Dispose();
             * }
             * }
             *
             * return state;
             */
        }