public StaDynVariablesInScopeTable getVariablesInCurrentScope(int line, int column, ITextSnapshot snapshot, int start, bool includeSSAVars)
        {
            var variablesInScope = new StaDynVariablesInScopeTable();

            StaDynSourceFileAST file = ProjectFileAST.Instance.getAstFile(FileUtilities.Instance.getCurrentOpenDocumentFilePath());

            if (file == null || file.Ast == null)
            {
                return(null);
            }

            //AstNode foundNode = (AstNode)file.Ast.Accept(new VisitorFindNode(), new Location(Path.GetFileName(file.FileName), line + 1, column));
            AstNode foundNode = (AstNode)file.Ast.Accept(new VisitorFindNode(), new Location(file.FileName, line + 1, column));

            if (foundNode == null)
            {
                return(null);
            }
            variablesInScope = file.Ast.Accept(new VisitorFindDefinitionsInScope(includeSSAVars), foundNode) as StaDynVariablesInScopeTable;

            if (variablesInScope == null)
            {
                return(variablesInScope);
            }

            /*
             * The foundNode is the next node that we actually are looking for.
             * So, we must pop as many groups of variables as closing braces we found
             * between foundeNode location and the location where the completion was triggered.
             */
            int blocksToPop = SourceHelper.countCharAppearances(snapshot, start, foundNode.Location.Line, foundNode.Location.Column, '}');

            for (int i = 0; i < blocksToPop; i++)
            {
                variablesInScope.Reset();
            }

            return(variablesInScope);
        }
Ejemplo n.º 2
0
        public static void DeclareExplicit(AstNode node, bool showMessageBox)
        {
            if (!(node is IdDeclaration))
            {
                return;
            }

            string varName    = ((IdDeclaration)node).Identifier;
            int    lineNumber = node.Location.Line;
            int    column     = node.Location.Column;

            var snapshot = FileUtilities.Instance.getCurrentTextSnapShot();

            //if (node is Definition) {
            //  if (((Definition)node).FrozenTypeExpression != null) {
            //    SourceHelper.replaceWord(snapshot, lineNumber - 1, "var", ((Definition)node).FrozenTypeExpression.FullName);
            //    return;
            //  }
            //}


            // int start = FileUtilities.Instance.getCurrentCaretPosition();
            int start = FileUtilities.Instance.getPositionFromSpan(lineNumber - 1, column);
            StaDynVariablesInScopeTable infoVariablesInScope = StaDynIntellisenseHelper.Instance.getVariablesInCurrentScope(lineNumber + 1, column, snapshot, start, true);

            //var SSAVariablesList = new List<Declaration>();
            var substitutionTypes = new HashSet <TypeExpression>();

            //Look for the same variable in the current scope
            for (int i = 0; i < infoVariablesInScope.Table.Count; i++)
            {
                if (infoVariablesInScope.Table[i].Count > 0)
                {
                    foreach (KeyValuePair <string, IdDeclaration> variable in infoVariablesInScope.Table[i])
                    {
                        //string SSAVarName="";
                        //var type = variable.Value.TypeExpr;

                        var type = ((Declaration)variable.Value).ILTypeExpression;

                        //if(variable.Key.Length>varName.Length)
                        //    SSAVarName = variable.Key.Substring(0, varName.Length);
                        //if (variable.Key.StartsWith(varName) && type is TypeVariable && ((TypeVariable)type).Substitution != null)

                        //if (SSAVarName.Equals(varName) && type is TypeVariable)
                        if (variable.Value.Identifier == varName && type != null)
                        {
                            //SSAVariablesList.Add(variable.Value);
                            substitutionTypes.UnionWith(TypeSystemHelper.Instance.getSubstitutionType(type));
                        }
                    }
                }
            }
            //if (SSAVariablesList.Count != 1)
            if (substitutionTypes.Count != 1)
            {
                string message;
                if (substitutionTypes.Count == 0)
                {
                    message = "The var reference named '" + varName +
                              "' cannot be declared explicitly since it has no type yet\n";
                }
                else
                {
                    message = "The var reference named '" + varName +
                              "' cannot be declared explicitly since it has more than one type within its scope:\n";
                    foreach (TypeExpression type in substitutionTypes)
                    {
                        message += " - " + type.FullName + "\n";
                    }
                    message += "To be able to declare explicitly this reference, create a new type from which "
                               + "all this types will inherit";
                }

                Trace.WriteLine(message);

                if (showMessageBox)
                {
                    MessageBox.Show(
                        message,
                        "Cannot declare explicit",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return;
            }

            ////var t = substitutionTypes.ElementAt(0);
            //TypeExpression t= substitutionTypes.ToList()[0];
            //string description = t.AcceptOperation(new GetTypeSystemName(), null) as string;
            //var changeType = description.Split(':');

            //SourceHelper.replaceWord(snapshot, lineNumber - 1, "var", changeType[changeType.Length -1]);
            SourceHelper.replaceWord(snapshot, lineNumber - 1, "var", substitutionTypes.ElementAt(0).FullName);
        }
Ejemplo n.º 3
0
        private List <Completion> getVariablesInScope(int lineNumber, int column, ITextSnapshot snapshot, int start)
        {
            List <Completion> variablesInScope = new List <Completion>();

            ///*
            // * The foundNode is the next node that we actually are looking for.
            // * So, we must pop as many groups of variables as closing braces we found
            // * between foundeNode location and the location where the completion was triggered.
            // */

            //int blocksToPop = SourceHelper.countCharAppearances(snapshot, start, foundNode.Location.Line, foundNode.Location.Column, '}');

            StaDynVariablesInScopeTable infoVariablesInScope = StaDynIntellisenseHelper.Instance.getVariablesInCurrentScope(lineNumber, column, snapshot, start, false);

            if (infoVariablesInScope == null)
            {
                return(variablesInScope);
            }
            //for (int i = 0; i < infoVariablesInScope.Table.Count; i++)
            //{
            //    if (infoVariablesInScope.Table[i].Count > 0)
            //    {
            //        foreach (KeyValuePair<string, TypeExpression> variable in infoVariablesInScope.Table[i])
            //        {
            //            //icon = CompletionGlyph.Instance.getIcon(variable.Value);
            //            ImageSource image = CompletionGlyph.Instance.getImage(variable.Value, this._glyphService);
            //            variablesInScope.Add(new Completion(variable.Key, variable.Key, variable.Value.FullName, image, variable.Key));
            //        }
            //    }
            //}
            string description;

            for (int i = 0; i < infoVariablesInScope.Table.Count; i++)
            {
                if (infoVariablesInScope.Table[i].Count > 0)
                {
                    foreach (KeyValuePair <string, IdDeclaration> variable in infoVariablesInScope.Table[i])
                    {
                        //icon = CompletionGlyph.Instance.getIcon(variable.Value);
                        description = variable.Value.TypeExpr != null ? variable.Value.TypeExpr.FullName : "";

                        ImageSource image = CompletionGlyph.Instance.getImage(variable.Value.TypeExpr, this._glyphService);
                        variablesInScope.Add(new Completion(variable.Key, variable.Key, description, image, variable.Key));
                    }
                }
            }

            //for (int i = 0; i < infoVariablesInScope.Table.Count - blocksToPop; i++)
            //{
            //    if (infoVariablesInScope.Table[i].Count > 0)
            //    {
            //        foreach (KeyValuePair<string, TypeExpression> variable in infoVariablesInScope.Table[i])
            //        {
            //            //icon = CompletionGlyph.Instance.getIcon(variable.Value);
            //            ImageSource image = CompletionGlyph.Instance.getImage(variable.Value, this._glyphService);
            //            variablesInScope.Add(new Completion(variable.Key, variable.Key, variable.Value.FullName, image, variable.Key));
            //        }
            //    }
            //}

            //Sort the elements
            variablesInScope.Sort((x, y) => string.Compare(x.DisplayText, y.DisplayText));

            return(variablesInScope);
        }