Example #1
0
        private static bool GetIfVariableState(string wordBefore2, string wordBefore3, string wordBefore4)
        {
            IElement element;

            if (string.IsNullOrWhiteSpace(wordBefore3))
            {
                element = EditorLogic.CurrentElement;
            }
            else
            {
                element = ObjectFinder.Self.GetElementUnqualified(wordBefore4);
            }
            if (element != null)
            {
                var  availableStates = StateCodeGenerator.GetAllStatesForCategory(element, wordBefore2);
                bool isCategory      = wordBefore2 == "VariableState" || availableStates.Count != 0;


                return((isCategory && wordBefore3 == "." && ObjectFinder.Self.GetElementUnqualified(wordBefore4) != null)
                       ||
                       (isCategory && string.IsNullOrWhiteSpace(wordBefore3)));
            }
            else
            {
                return(false);
            }
        }
        public async IAsyncEnumerable <EntityState> GetStatesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            var response = await HttpClient.GetAsync("states", HttpCompletionOption.ResponseHeadersRead, cancellationToken);

            response.EnsureSuccessStatusCode();

            var jsonSerializer = JsonSerializer.Create(JsonSerializerSettings);

            using (var responseStream = await response.Content.ReadAsStreamAsync())
                using (var streamReader = new StreamReader(responseStream))
                    using (var reader = new JsonTextReader(streamReader))
                    {
                        while (await reader.ReadAsync(cancellationToken))
                        {
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                var jObject = await JObject.LoadAsync(reader, cancellationToken);

                                var entityId   = jObject["entity_id"]?.Value <string>() ?? throw new InvalidOperationException();
                                var state      = jObject["state"]?.ToString();
                                var attributes = jObject["attributes"]?.ToObject <Dictionary <string, object> >() ?? throw new InvalidOperationException();

                                StateCodeGenerator.Observe(entityId, state, attributes);

                                var entityStateType = StateProvider.GetEntityStateType(entityId, attributes);
                                var entityState     = (EntityState)jObject.ToObject(entityStateType, jsonSerializer);

                                yield return(entityState);
                            }
                        }
                    }
        }
Example #3
0
        public void TestStateCodeGeneration()
        {
            ICodeBlock codeBlock = new CodeDocument(0);

            mButtonInButtonList.CurrentState = "InvalidState";

            StateCodeGenerator.WriteSetStateOnNamedObject(mButtonInButtonList, codeBlock);
            string result = codeBlock.ToString();

            if (result.Contains(mButtonInButtonList.CurrentState))
            {
                throw new Exception("Code generation for NamedObjects is generating state setting code when states don't really exist");
            }

            // Make sure generation doesn't mess up on a entity with a "" base (instead of null)
            StateCodeGenerator scg        = new StateCodeGenerator();
            EntitySave         entitySave = new EntitySave();

            entitySave.States.Add(new StateSave());

            entitySave.BaseEntity = "";
            scg.GenerateFields(codeBlock, entitySave);
        }
Example #4
0
        public List <string> GetAutoCompleteValues()
        {
            string textBeforePeriod = "";

            Alsing.SourceCode.Row row = syntaxBoxControl1.Caret.CurrentRow;

            int indexIntoRow = syntaxBoxControl1.Caret.Position.X;

            List <string> toReturn = new List <string>();

            try
            {
                if (EditorLogic.CurrentElement != null)
                {
                    if (syntaxBoxControl1.Caret.PreviousWord != null)
                    {
                        string wordBeforeDot = syntaxBoxControl1.Caret.PreviousWord.Text;
                        string wordBefore2   = syntaxBoxControl1.Caret.GetWordText(1);
                        string wordBefore3   = syntaxBoxControl1.Caret.GetWordText(2);
                        string wordBefore4   = syntaxBoxControl1.Caret.GetWordText(3);

                        Type type;

                        if (wordBefore2 == "this")
                        {
                            IElement element = EditorLogic.CurrentElement;

                            AddAutoCompleteForElement(toReturn, element);
                        }
                        else if (EditorLogic.CurrentElement.GetNamedObjectRecursively(wordBefore2) != null)
                        {
                            NamedObjectSave nos = EditorLogic.CurrentElement.GetNamedObjectRecursively(wordBeforeDot);

                            AddAutoCompleteFor(toReturn, nos);
                        }
                        else if (EditorLogic.CurrentElement.GetCustomVariableRecursively(wordBefore2) != null)
                        {
                            CustomVariable customVariable = EditorLogic.CurrentElement.GetCustomVariableRecursively(wordBefore2);
                            AddAutoCompleteFor(toReturn, customVariable);
                        }
                        else if (ObjectFinder.Self.GetElementUnqualified(wordBefore2) != null)
                        {
                            AddStaticAutoCompleteFor(toReturn, ObjectFinder.Self.GetElementUnqualified(wordBeforeDot));
                        }
                        else if (GetIfVariableState(wordBefore2, wordBefore3, wordBefore4))
                        {
                            IElement element = null;

                            if (string.IsNullOrWhiteSpace(wordBefore3))
                            {
                                element = EditorLogic.CurrentElement;
                            }
                            else
                            {
                                element = ObjectFinder.Self.GetElementUnqualified(wordBefore4);
                            }

                            var availableStates = StateCodeGenerator.GetAllStatesForCategory(element, wordBefore2);

                            if (element.GetStateCategory(wordBefore2) == null)
                            {
                                availableStates = StateCodeGenerator.GetSharedVariableStates(element);
                            }

                            foreach (StateSave stateSave in availableStates)
                            {
                                toReturn.Add(stateSave.Name);
                            }
                        }
                        else if ((type = TypeManager.GetFlatRedBallType(wordBefore2)) != null)
                        {
                            AddStaticAutoCompleteFor(toReturn, type);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                int m = 3;
            }
            StringFunctions.RemoveDuplicates(toReturn);
            toReturn.Sort();
            return(toReturn);
        }