示例#1
0
 public virtual void Call(ScriptFile script, DocRange callRange)
 {
     if (Description != null)
     {
         script.AddHover(callRange, Description);
     }
 }
        string GetImportedFile(ScriptFile script, FileImporter importer, DeltinScriptParser.Import_fileContext importFileContext)
        {
            DocRange stringRange = DocRange.GetRange(importFileContext.STRINGLITERAL());

            var importResult = importer.Import(
                stringRange,
                Extras.RemoveQuotes(importFileContext.STRINGLITERAL().GetText()),
                script.Uri
                );

            if (!importResult.SuccessfulReference)
            {
                return(importResult.Directory);
            }

            script.AddDefinitionLink(stringRange, new Location(importResult.Uri, DocRange.Zero));
            script.AddHover(stringRange, importResult.FilePath);

            if (importResult.ShouldImport)
            {
                ScriptFile importedScript = new ScriptFile(Diagnostics, importResult.Uri, FileGetter.GetScript(importResult.Uri));
                CollectScriptFiles(importedScript);
            }
            return(importResult.Directory);
        }
 // ICallable
 public void Call(ScriptFile script, DocRange callRange)
 {
     ThrowIfNotFinalized();
     script.AddDefinitionLink(callRange, DefinedAt);
     script.AddHover(callRange, GetLabel(true));
     parseInfo.TranslateInfo.AddSymbolLink(this, new Location(script.Uri, callRange));
 }
示例#4
0
        public override object Validate(ScriptFile script, IExpression value, DocRange valueRange)
        {
            StringAction str = value as StringAction;

            if (str == null)
            {
                script.Diagnostics.Error("Expected string constant.", valueRange);
                return(null);
            }

            string resultingPath = Extras.CombinePathWithDotNotation(script.Uri.FilePath(), str.Value);

            if (resultingPath == null)
            {
                script.Diagnostics.Error("File path contains invalid characters.", valueRange);
                return(null);
            }

            string dir = Path.GetDirectoryName(resultingPath);

            if (Directory.Exists(dir))
            {
                Importer.AddImportCompletion(script, dir, valueRange);
            }

            if (!File.Exists(resultingPath))
            {
                script.Diagnostics.Error($"No file was found at '{resultingPath}'.", valueRange);
                return(null);
            }

            if (FileTypes != null && !FileTypes.Contains(Path.GetExtension(resultingPath).ToLower()))
            {
                script.Diagnostics.Error($"Expected a file with the file type '{string.Join(", ", FileTypes)}'.", valueRange);
                return(null);
            }

            script.AddDefinitionLink(valueRange, new Location(Extras.Definition(resultingPath), DocRange.Zero));
            script.AddHover(valueRange, resultingPath);

            return(resultingPath);
        }
示例#5
0
        public override void Call(ScriptFile script, DocRange callRange)
        {
            MarkupBuilder hoverContents = new MarkupBuilder();

            if (Constant() == TypeSettable.Convertable)
            {
                hoverContents
                .StartCodeLine()
                .Add("enum " + Name)
                .EndCodeLine();
            }
            else if (Constant() == TypeSettable.Constant)
            {
                hoverContents
                .StartCodeLine()
                .Add("constant " + Name)
                .EndCodeLine()
                .NewSection()
                .Add("Constant workshop types cannot be stored. Variables with this type cannot be changed from their initial value.");
            }

            script.AddHover(callRange, hoverContents.ToString());
        }
        string GetImportedFile(DeltinScript deltinScript, ScriptFile script, FileImporter importer, Import importFileContext)
        {
            // If the file being imported is being imported as an object, get the variable name.
            string variableName = importFileContext.Identifier?.Text;

            DocRange stringRange = importFileContext.File.Range;

            ImportResult importResult = importer.Import(
                stringRange,
                importFileContext.File.Text.RemoveQuotes(),
                script.Uri
                );

            if (!importResult.SuccessfulReference)
            {
                return(importResult.Directory);
            }

            // Add hover and definition info.
            script.AddDefinitionLink(stringRange, new Location(importResult.Uri, DocRange.Zero));
            script.AddHover(stringRange, importResult.FilePath);

            if (importResult.ShouldImport)
            {
                // Import the file if it should be imported.
                if (variableName == null)
                {
                    switch (importResult.FileType)
                    {
                    // Get script file.
                    case ".del":
                    case ".ostw":
                    case ".workshop":
                        ScriptFile importedScript = new ScriptFile(_diagnostics, _fileGetter.GetScript(importResult.Uri));
                        CollectScriptFiles(deltinScript, importedScript);
                        break;

                    // Get lobby settings.
                    case ".json":
                    case ".lobby":
                        JObject lobbySettings = null;

                        // Make sure the json is in the correct format.
                        try
                        {
                            ImportedScript file = _fileGetter.GetImportedFile(importResult.Uri);
                            file.Update();

                            // Convert the json to a jobject.
                            lobbySettings = JObject.Parse(file.Content);

                            // An exception will be thrown if the jobject cannot be converted to a Ruleset.
                            lobbySettings.ToObject(typeof(Ruleset));

                            if (!Ruleset.Validate(lobbySettings, script.Diagnostics, stringRange))
                            {
                                break;
                            }
                        }
                        catch
                        {
                            // Error if the json failed to parse.
                            script.Diagnostics.Error("Failed to parse the settings file.", stringRange);
                            break;
                        }

                        // If no lobby settings were imported yet, set MergedLobbySettings to the jobject.
                        if (MergedLobbySettings == null)
                        {
                            MergedLobbySettings = lobbySettings;
                        }
                        else
                        {
                            // Otherwise, merge current lobby settings.
                            lobbySettings.Merge(MergedLobbySettings, new JsonMergeSettings
                            {
                                MergeArrayHandling     = MergeArrayHandling.Union,
                                MergeNullValueHandling = MergeNullValueHandling.Ignore
                            });
                            MergedLobbySettings = lobbySettings;
                        }
                        break;
                    }
                }
                else
                {
                    switch (importResult.FileType)
                    {
                    case ".json":
                        ImportedScript file = _fileGetter.GetImportedFile(importResult.Uri);
                        file.Update();

                        JObject     jsonData = JObject.Parse(file.Content);
                        InternalVar jsonVar  = new InternalVar(variableName, new JsonType(jsonData));

                        if (((JsonType)jsonVar.CodeType).ContainsDeepArrays())
                        {
                            script.Diagnostics.Error("JSON Arrays cannot include objects or arrays.", stringRange);
                        }

                        _deltinScript.RulesetScope.AddVariable(jsonVar, script.Diagnostics, importFileContext.Identifier.Range);
                        _deltinScript.DefaultIndexAssigner.Add(jsonVar, Element.Null());
                        break;
                    }
                }
            }
            return(importResult.Directory);
        }
        string GetImportedFile(ScriptFile script, FileImporter importer, DeltinScriptParser.Import_fileContext importFileContext)
        {
            DocRange stringRange = DocRange.GetRange(importFileContext.STRINGLITERAL());

            ImportResult importResult = importer.Import(
                stringRange,
                Extras.RemoveQuotes(importFileContext.STRINGLITERAL().GetText()),
                script.Uri
                );

            if (!importResult.SuccessfulReference)
            {
                return(importResult.Directory);
            }

            // Add hover and definition info.
            script.AddDefinitionLink(stringRange, new Location(importResult.Uri, DocRange.Zero));
            script.AddHover(stringRange, importResult.FilePath);

            if (importResult.ShouldImport)
            {
                // Import the file if it should be imported.
                switch (importResult.FileType)
                {
                // Get script file.
                case ".del":
                case ".ostw":
                case ".workshop":
                    ScriptFile importedScript = new ScriptFile(Diagnostics, importResult.Uri, FileGetter.GetScript(importResult.Uri));
                    CollectScriptFiles(importedScript);
                    break;

                // Get lobby settings.
                case ".json":
                    JObject lobbySettings = null;

                    // Make sure the json is in the correct format.
                    try
                    {
                        ImportedScript file = FileGetter.GetImportedFile(importResult.Uri);
                        file.Update();

                        // Convert the json to a jobject.
                        lobbySettings = JObject.Parse(file.Content);

                        // An exception will be thrown if the jobject cannot be converted to a Ruleset.
                        lobbySettings.ToObject(typeof(Ruleset));

                        if (!Ruleset.Validate(lobbySettings, script.Diagnostics, stringRange))
                        {
                            break;
                        }
                    }
                    catch
                    {
                        // Error if the json failed to parse.
                        script.Diagnostics.Error("Failed to parse the settings file.", stringRange);
                        break;
                    }

                    // If no lobby settings were imported yet, set MergedLobbySettings to the jobject.
                    if (MergedLobbySettings == null)
                    {
                        MergedLobbySettings = lobbySettings;
                    }
                    else
                    {
                        // Otherwise, merge current lobby settings.
                        lobbySettings.Merge(MergedLobbySettings, new JsonMergeSettings {
                            MergeArrayHandling     = MergeArrayHandling.Union,
                            MergeNullValueHandling = MergeNullValueHandling.Ignore
                        });
                        MergedLobbySettings = lobbySettings;
                    }
                    break;
                }
            }
            return(importResult.Directory);
        }
 public virtual void Call(ScriptFile script, DocRange callRange)
 {
     script.AddHover(callRange, GetLabel(true));
 }
示例#9
0
 /// <summary>Calls a type from the specified document range.</summary>
 /// <param name="script">The script that the type was called from.</param>
 /// <param name="callRange">The range of the call.</param>
 public virtual void Call(ScriptFile script, DocRange callRange)
 {
     script.AddHover(callRange, HoverHandler.Sectioned(Kind + " " + Name, Description));
 }