Esempio n. 1
0
        /// <summary>
        /// Methods
        /// </summary>
        public void Visit(ParsedMethod pars)
        {
            pars.ReturnType = ConvertStringToParsedPrimitiveType(pars.TempReturnType, false);

            // to code explorer
            var classNode  = GetExplorerListNode(pars.ClassName, CodeExplorerIconType.Block);
            var parentNode = GetExplorerListNode("Methods", CodeExplorerIconType.Method, classNode);
            var newNode    = CodeItem.Factory.New(CodeExplorerIconType.Method);

            newNode.DisplayText   = pars.Name;
            newNode.Flags         = pars.Flags;
            newNode.DocumentOwner = pars.FilePath;
            newNode.GoToLine      = pars.Line;
            newNode.GoToColumn    = pars.Column;
            PushToCodeExplorer(parentNode, newNode);

            // to completion data
            var method = CompletionItem.Factory.New(CompletionType.Method);

            method.DisplayText    = pars.Name;
            method.ParsedBaseItem = pars;
            method.FromParser     = true;
            method.SubText        = null;
            method.Ranking        = AutoCompletion.FindRankingOfParsedItem(pars.Name);
            method.Flags          = pars.Flags;
            PushToAutoCompletion(method, pars);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns the start and end index in the argument fileContents of the entire method including the header of the method
        /// and the opening/closing brackets.
        /// </summary>
        /// <param name="parsedMethod">The parsed method.</param>
        /// <param name="fileContents">The contents of the entire file.</param>
        /// <param name="startIndex">The found startIndex.</param>
        /// <param name="endIndex">The found endIndex which includes the closing bracket.</param>
        public static void GetStartAndEndIndexForMethod(ParsedMethod parsedMethod, string fileContents, out int startIndex, out int endIndex)
        {
            string name = parsedMethod.Name;


            GetStartAndEndIndexForMethod(fileContents, name, out startIndex, out endIndex);
        }
Esempio n. 3
0
        public static void AddStubsForCustomEvents(IElement element)
        {
            // EARLY OUT///////
            if (element.Events.Count == 0)
            {
                return;
            }
            ////////EARLY OUT/////////

            string file = element.Events[0].GetSharedCodeFullFileName();

            if (File.Exists(file))
            {
                ParsedClass parsedClass = GetParsedClassFrom(file);

                if (parsedClass == null)
                {
                    // this file is empty
                    foreach (EventResponseSave ers in element.Events)
                    {
                        try
                        {
                            InjectTextForEventAndSaveCustomFile(element, ers, "");
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Failed to generate custom code stubs for event " + ers.EventName + "\n\n" + e);
                        }
                    }
                }
                else
                {
                    foreach (EventResponseSave ers in element.Events)
                    {
                        ParsedMethod parsedMethod = parsedClass.GetMethod("On" + ers.EventName);

                        if (parsedMethod == null)
                        {
                            // This method doesn't exist.  This will cause an error
                            // because generated code expects this to always exist. Let's
                            // make an empty stub

                            InjectTextForEventAndSaveCustomFile(element, ers, "");
                        }
                    }
                }
            }
            else
            {
                // The file doesn't exist, we gotta generate it
            }
        }
Esempio n. 4
0
        private string[] GetMethodLines(IElement element, EventResponseSave ers, string projectDirectory)
        {
            string[] toReturn = null;
            lock (mCachedMethodLines)
            {
                if (!mCachedMethodLines.ContainsKey(ers.EventName))
                {
                    ParsedMethod parsedMethod =
                        ers.GetParsedMethodFromAssociatedFile(element, projectDirectory);
                    string[] lines = parsedMethod.MethodContents.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    mCachedMethodLines.Add(ers.EventName, lines);
                }

                toReturn = mCachedMethodLines[ers.EventName];
            }
            return(toReturn);
        }
Esempio n. 5
0
        public static string GetEventContents(this EventResponseSave instance)
        {
            IElement element = instance.GetContainer();

            string textToAssign = null;

            if (FacadeContainer.Self.GlueState.CurrentEventResponseSave != null)
            {
                if (!string.IsNullOrEmpty(instance.Contents))
                {
                    textToAssign = instance.Contents;
                }
                else
                {
                    // Is there a non-Generated.Event.cs file?
                    string fileToLookFor = FileManager.RelativeDirectory +
                                           EventResponseSave.GetEventFileNameForElement(element);

                    if (File.Exists(fileToLookFor))
                    {
                        ParsedMethod parsedMethod =
                            instance.GetParsedMethodFromAssociatedFile();

                        if (parsedMethod != null)
                        {
                            textToAssign =
                                parsedMethod.MethodContents;
                        }
                    }
                }
            }
            else
            {
                textToAssign = null;
            }
            return(textToAssign);
        }
Esempio n. 6
0
        /// <summary>
        /// Injects the insideOfMethod into an event for the argument
        /// </summary>
        /// <param name="currentElement">The IElement containing the EventResponseSave.</param>
        /// <param name="eventResponseSave">The EventResponseSave which should have its contents set or replaced.</param>
        /// <param name="insideOfMethod">The inside of the methods to assign.</param>
        /// <returns>The full file name which contains the method contents.</returns>
        public static string InjectTextForEventAndSaveCustomFile(IElement currentElement, EventResponseSave eventResponseSave, string insideOfMethod)
        {
            // In case the user passes null we don't want to have null reference exceptions:
            if (insideOfMethod == null)
            {
                insideOfMethod = "";
            }

            ParsedMethod parsedMethod =
                eventResponseSave.GetParsedMethodFromAssociatedFile();


            string fullFileName = eventResponseSave.GetSharedCodeFullFileName();

            bool forceRegenerate = false;

            string fileContents = null;

            if (File.Exists(fullFileName))
            {
                fileContents    = FileManager.FromFileText(fullFileName);
                forceRegenerate = fileContents.Contains("public partial class") == false && fileContents.Contains("{") == false;

                if (forceRegenerate)
                {
                    GlueGui.ShowMessageBox("Forcing a regneration of " + fullFileName + " because it appears to be empty.");
                }
            }

            CreateEmptyCodeIfNecessary(currentElement, fullFileName, forceRegenerate);

            fileContents = FileManager.FromFileText(fullFileName);

            int indexToAddAt = 0;

            if (parsedMethod != null)
            {
                int startIndex;
                int endIndex;
                GetStartAndEndIndexForMethod(parsedMethod, fileContents, out startIndex, out endIndex);
                // We want to include the \r\n at the end, so add 2
                endIndex += 2;
                string whatToRemove = fileContents.Substring(startIndex, endIndex - startIndex);

                fileContents = fileContents.Replace(whatToRemove, null);

                indexToAddAt = startIndex;
                // remove the method to re-add it
            }
            else
            {
                indexToAddAt = EventManager.GetLastLocationInClass(fileContents, startOfLine: true);
            }
            ICodeBlock codeBlock = new CodeDocument(2);

            codeBlock.TabCharacter = "    ";

            insideOfMethod = "" + insideOfMethod.Replace("\r\n", "\r\n            ");
            codeBlock      = EventCodeGenerator.FillWithCustomEventCode(codeBlock, eventResponseSave, insideOfMethod, currentElement);

            string methodContents = codeBlock.ToString();


            fileContents = fileContents.Insert(indexToAddAt, codeBlock.ToString());

            eventResponseSave.Contents = null;
            try
            {
                FlatRedBall.Glue.IO.FileWatchManager.IgnoreNextChangeOnFile(fullFileName);
                FileManager.SaveText(fileContents, fullFileName);
            }
            catch (Exception e)
            {
                PluginManager.ReceiveError("Could not save file to " + fullFileName);
            }
            return(fullFileName);
        }
Esempio n. 7
0
 public void Visit(ParsedMethod pars)
 {
     AppendEverything(pars);
 }
        public void UpdateDisplayToCurrentObject()
        {
            string fullFileName = "<Unable to get file name>";

            try
            {
                EventResponseSave eventResponseSave = EditorLogic.CurrentEventResponseSave;
                IElement          element           = EditorLogic.CurrentElement;

                fullFileName = eventResponseSave.GetSharedCodeFullFileName();

                string contents = FileManager.FromFileText(fullFileName);

                CSharpParser parser     = new CSharpParser();
                SyntaxTree   syntaxTree = parser.Parse(contents);
                mIsCodeValid = syntaxTree.Errors.Count == 0;

                if (mIsCodeValid)
                {
                    ParsedMethod parsedMethod = eventResponseSave.GetParsedMethodFromAssociatedFile();

                    string textToAssign = null;
                    if (parsedMethod == null)
                    {
                        textToAssign = eventResponseSave.GetEventContents();
                    }
                    else
                    {
                        StringBuilderDocument document = new StringBuilderDocument(contents);

                        bool wasFound;

                        textToAssign = GetMethodContentsFor(syntaxTree, document, eventResponseSave.EventName, out wasFound);
                        if (wasFound)
                        {
                            textToAssign = textToAssign.Replace("\n", "\r\n");
                        }
                        else
                        {
                            mIsCodeValid = false;
                            textToAssign = "Could not find the method, or encountered a parse error.";
                        }
                    }

                    textToAssign = RemoveWhiteSpaceForCodeWindow(textToAssign);
                    this.syntaxBoxControl1.Document.Text = textToAssign;
                    mLastSavedText = this.syntaxBoxControl1.Document.Text;
                }
                else
                {
                    this.syntaxBoxControl1.Document.Text = "This code file is not a complete code file:\n" +
                                                           fullFileName +
                                                           "\nGlue is unable to parse it.  Please correct the problems in Visual Studio";
                }
            }
            catch (Exception e)
            {
                mIsCodeValid = false;
                this.syntaxBoxControl1.Document.Text = "Error parsing file:\n" +
                                                       fullFileName +
                                                       "\nMore details:\n\n" + e.ToString();
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Matches a procedure definition
        /// </summary>
        private ParsedMethod CreateParsedMethod(Token methodToken, ParsedScope parentScope)
        {
            /*
             * METHOD [ PRIVATE | PROTECTED | PUBLIC ] [ STATIC | ABSTRACT ]
             *  [ OVERRIDE ] [ FINAL ]
             *  { VOID | return-type } method-name
             *  ( [ parameter [ , parameter ] ... ] ) :
             */

            // info we will extract from the current statement :
            string              name             = "";
            string              parsedReturnType = "CLASS";
            ParseFlag           flags            = 0;
            StringBuilder       parameters       = new StringBuilder();
            List <ParsedDefine> parametersList   = null;

            Token token;
            int   state = 0;

            do
            {
                token = PeekAt(1); // next token
                if (token is TokenEos)
                {
                    break;
                }
                if (token is TokenComment)
                {
                    continue;
                }
                switch (state)
                {
                case 0:
                    // default state
                    if (token is TokenWord)
                    {
                        switch (token.Value.ToLower())
                        {
                        case "private":
                            flags |= ParseFlag.Private;
                            break;

                        case "public":
                            flags |= ParseFlag.Public;
                            break;

                        case "protected":
                            flags |= ParseFlag.Protected;
                            break;

                        case "static":
                            flags |= ParseFlag.Static;
                            break;

                        case "abstract":
                            flags |= ParseFlag.Abstract;
                            break;

                        case "override":
                            flags |= ParseFlag.Override;
                            break;

                        case "final":
                            flags |= ParseFlag.Final;
                            break;

                        case "void":
                        case "class":
                        case "character":
                        case "integer":
                        case "int64":
                        case "decimal":
                        case "date":
                        case "datetime":
                        case "datetime-tz":
                        case "handle":
                        case "logical":
                        case "longchar":
                        case "memptr":
                        case "recid":
                        case "rowid":
                        case "raw":
                            parsedReturnType = token.Value.ToUpper();
                            break;

                        default:
                            name = token.Value;
                            break;
                        }
                    }
                    if (token is TokenSymbol && token.Value.Equals("("))
                    {
                        state = 1;
                    }
                    break;

                case 1:
                    // read parameters, define a ParsedDefineItem for each
                    parametersList = GetParsedParameters(methodToken, parameters);
                    state          = 2;
                    break;
                }
            } while (MoveNext());

            if (state < 1)
            {
                return(null);
            }

            var newMethod = new ParsedMethod(name, methodToken, parentScope, parsedReturnType)
            {
                // = end position of the EOS of the statement
                Flags       = flags,
                EndPosition = token.EndPosition
            };

            if (parametersList != null)
            {
                newMethod.Parameters = new List <ParsedDefine>();
                foreach (var parsedItem in parametersList)
                {
                    newMethod.Parameters.Add(parsedItem);
                }
            }
            AddParsedItem(newMethod, methodToken.OwnerNumber);

            return(newMethod);
        }