Beispiel #1
0
 public StructAssigner(StructInstance structInstance, StructAssigningAttributes attributes, bool isArray)
 {
     _variables  = structInstance.Variables;
     _attributes = attributes;
     _isArray    = isArray;
 }
Beispiel #2
0
        void GetValue()
        {
            Name = "{";

            string[] variableNames = new string[_context.Values.Count];

            // Empty struct error.
            if (_context.Values.Count == 0)
            {
                _parseInfo.Script.Diagnostics.Error("Empty structs are not allowed", _context.Range);
            }

            var scopeHandler = new StructValueScopeHandler(_scope);

            // Create the struct type from the values.
            Variables = new IVariable[_context.Values.Count];
            for (int i = 0; i < _context.Values.Count; i++)
            {
                variableNames[i] = _context.Values[i].Identifier?.Text;

                // Separate variable names with a comma.
                if (i > 0)
                {
                    Name += ", ";
                }

                // No type in an explicit struct declaration.
                if (_isExplicit && _context.Values[i].Type == null)
                {
                    _parseInfo.Script.Diagnostics.Error("Inconsistent struct value usage; value types must be all explicit or all implicit", _context.Values[i].Identifier.Range);
                }

                // Create the struct variable.
                Variables[i] = new StructValueVariable(scopeHandler, new StructValueContextHandler(_parseInfo, _context.Values[i])).GetVar();

                // Add the variable label.
                Name += Variables[i].GetDefaultInstance(null).GetLabel(_parseInfo.TranslateInfo);
            }

            Name += "}";

            // Add completion.
            if (_parseInfo.ExpectingType is StructInstance expectingStruct)
            {
                // Create completions from the expected struct's variables.
                var completions = expectingStruct.Variables
                                  // Do not add the completion if the variable already exists in the struct.
                                  .Where(expectingVariable => !variableNames.Contains(expectingVariable.Name))
                                  // Convert to CompletionItem.
                                  .Select(expectingVariable => expectingVariable.GetCompletion(_parseInfo.TranslateInfo));

                // Add completion range to script.
                _parseInfo.Script.AddCompletionRange(new CompletionRange(
                                                         deltinScript: _parseInfo.TranslateInfo,
                                                         completionItems: completions.ToArray(),
                                                         range: _context.Range,
                                                         kind: CompletionRangeKind.ClearRest
                                                         ));
            }

            Type = new StructInstance(this, InstanceAnonymousTypeLinker.Empty);
        }