Skip to content

Commit

Permalink
Merge pull request #27 from penev92/resolveInheritance
Browse files Browse the repository at this point in the history
Resolve inheritance, add hover info for node removal
  • Loading branch information
penev92 authored Oct 17, 2022
2 parents fe2abca + 7d1f2f5 commit 36a785a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/OpenRA.MiniYamlParser/MiniYaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace OpenRA.MiniYamlParser
{
Expand Down Expand Up @@ -91,5 +92,11 @@ public IEnumerable<string> ToLines(string key, string comment = null)
foreach (var line in Nodes.ToLines())
yield return "\t" + line;
}

public static List<MiniYamlNode> Load(IEnumerable<string> files)
{
var yaml = files.Select(f => MiniYamlLoader.FromFile(f));
return MiniYamlMerger.Merge(yaml);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using LspTypes;
using OpenRA.MiniYamlParser;
using Oraide.Core;
using Oraide.Core.Entities;
using Oraide.Core.Entities.MiniYaml;
Expand Down Expand Up @@ -66,6 +68,14 @@ protected virtual bool TryGetCursorTarget(TextDocumentPositionParams positionPar
var (fileNodes, flattenedNodes, fileLines) = openFileCache[fileUri.AbsoluteUri];

var targetLine = fileLines[targetLineIndex];

// If the target line is a comment we probably don't care about it - bail out early.
if (Regex.IsMatch(targetLine, "^\\s#"))
{
target = default;
return false;
}

var pre = targetLine.Substring(0, targetCharacterIndex);
var post = targetLine.Substring(targetCharacterIndex);

Expand Down Expand Up @@ -224,6 +234,22 @@ protected bool TryGetTargetStringIndentation(YamlNode yamlNode, out int indentat
return true;
}

protected bool TryMergeYamlFiles(IEnumerable<string> filePaths, out List<MiniYamlNode> nodes)
{
// As long as the merging passes there's a good chance we really do have a node like the target one to be removed.
// If the target node removal doesn't have a corresponding node addition (is invalid), MiniYaml loading will throw.
try
{
nodes = OpenRA.MiniYamlParser.MiniYaml.Load(filePaths);
return true;
}
catch (Exception)
{
nodes = null;
return false;
}
}

protected string NormalizeFilePath(string filePath)
{
// Because VSCode sends us weird partially-url-encoded file paths.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using LspTypes;
using Oraide.Core;
using Oraide.Core.Entities;
Expand Down Expand Up @@ -112,6 +113,14 @@ protected override bool TryGetCursorTarget(TextDocumentPositionParams positionPa
var (fileNodes, flattenedNodes, fileLines) = openFileCache[fileUri.AbsoluteUri];

var targetLine = fileLines[targetLineIndex];

// If the target line is a comment we probably don't care about it - bail out early.
if (Regex.IsMatch(targetLine, "^\\s#"))
{
target = default;
return false;
}

var pre = targetLine.Substring(0, targetCharacterIndex);

var targetNode = flattenedNodes[targetLineIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ protected override Hover HandleRulesKey(CursorTarget cursorTarget)
return HoverFromHoverInfo(content, range);
}

if (cursorTarget.TargetString[0] == '-')
{
traitInfoName = traitInfoName.Substring(1);
if (codeSymbols.TraitInfos.Contains(traitInfoName))
{
var modData = symbolCache[cursorTarget.ModId];
var fileList = modData.ModManifest.RulesFiles;
var resolvedFileList = fileList.Select(x => OpenRaFolderUtils.ResolveFilePath(x, (modData.ModId, modData.ModFolder)));

if (TryMergeYamlFiles(resolvedFileList, out _))
return HoverFromHoverInfo($"Removes trait `{cursorTarget.TargetString.Substring(1)}` from the actor.", range);
}
}

return null;
}

Expand Down Expand Up @@ -267,6 +281,16 @@ protected override Hover HandleWeaponKey(CursorTarget cursorTarget)
if (prop.Name != null)
return HoverFromHoverInfo(prop.ToMarkdownInfoString(), range);

if (cursorTarget.TargetString[0] == '-')
{
var modData = symbolCache[cursorTarget.ModId];
var fileList = modData.ModManifest.WeaponsFiles;
var resolvedFileList = fileList.Select(x => OpenRaFolderUtils.ResolveFilePath(x, (modData.ModId, modData.ModFolder)));

if (TryMergeYamlFiles(resolvedFileList, out _))
return HoverFromHoverInfo($"Removes `{cursorTarget.TargetNode.Key.Substring(1)}` from the weapon.", range);
}

return null;
}

Expand Down

0 comments on commit 36a785a

Please sign in to comment.