Skip to content

Commit f891044

Browse files
committed
add code note hover text
1 parent 27d41d9 commit f891044

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

src/BufferManager.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
11
using System.Collections.Concurrent;
22
using System.Text;
3+
using System.Threading.Tasks;
4+
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
5+
using RASharp;
6+
using RASharp.Models;
37

48
namespace RAScriptLanguageServer
59
{
610
internal class BufferManager
711
{
812
private ConcurrentDictionary<string, RAScript> _buffers = new ConcurrentDictionary<string, RAScript>();
13+
private RetroAchievements? _retroachievements;
914

10-
public void UpdateBuffer(string documentPath, StringBuilder buffer, Parser p)
15+
public async Task UpdateBufferAsync(string documentPath, StringBuilder buffer, Parser p)
1116
{
17+
bool updateCodeNotes = true;
18+
RAScript? oldScript = this.GetBuffer(documentPath);
19+
if (oldScript != null)
20+
{
21+
int oldGameID = oldScript.GetParser().GetGameID();
22+
int newGameID = p.GetGameID();
23+
if (oldGameID == newGameID)
24+
{
25+
updateCodeNotes = false;
26+
}
27+
}
28+
GetCodeNotes? codeNotes = oldScript?.GetParser().GetCodeNotes();
29+
if (updateCodeNotes)
30+
{
31+
if (this._retroachievements != null && p.GetGameID() > 0)
32+
{
33+
try
34+
{
35+
codeNotes = await this._retroachievements.GetCodeNotes(p.GetGameID());
36+
}
37+
catch (Exception)
38+
{
39+
codeNotes = null;
40+
}
41+
}
42+
}
43+
p.loadCodeNotes(codeNotes);
1244
RAScript rascript = new RAScript(documentPath, buffer, p);
1345
_buffers.AddOrUpdate(documentPath, rascript, (k, v) => rascript);
1446
}
1547

48+
public void SetRAClient(RetroAchievements retroAchievements)
49+
{
50+
this._retroachievements = retroAchievements;
51+
}
52+
1653
public RAScript? GetBuffer(string documentPath)
1754
{
1855
return _buffers.TryGetValue(documentPath, out var buffer) ? buffer : null;

src/Parser.cs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
using OmniSharp.Extensions.LanguageServer.Protocol;
21
using System.Text.RegularExpressions;
32
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
4-
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
53
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
6-
using System.Collections.Generic;
74
using System.Text;
5+
using RASharp.Models;
6+
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
87

98
namespace RAScriptLanguageServer
109
{
1110
public class Parser
1211
{
13-
private readonly ILanguageServerFacade _router;
12+
public readonly ILanguageServerFacade _router;
1413
private readonly string text;
1514
private readonly TextPositions textPositions;
1615
private readonly Dictionary<string, Position> functionLocations;
1716
private readonly Dictionary<string, string[]> comments;
1817
private readonly Dictionary<string, CompletionItemKind> keywordKinds;
1918
private readonly List<string> keywords;
2019
private readonly FunctionDefinition[] functionDefinitions;
20+
private int gameID;
21+
private GetCodeNotes? codeNotes;
2122

2223
public Parser(ILanguageServerFacade router, FunctionDefinitions functionDefinitions, string text)
2324
{
@@ -29,6 +30,7 @@ public Parser(ILanguageServerFacade router, FunctionDefinitions functionDefiniti
2930
this.keywordKinds = new Dictionary<string, CompletionItemKind>();
3031
this.keywords = new List<string>();
3132
this.functionDefinitions = functionDefinitions.functionDefinitions;
33+
this.gameID = 0; // game id's start at 1 on RA
3234
this.Load();
3335
Dictionary<string, CompletionItemKind>.KeyCollection keyColl = this.keywordKinds.Keys;
3436
foreach (string k in keyColl)
@@ -37,6 +39,29 @@ public Parser(ILanguageServerFacade router, FunctionDefinitions functionDefiniti
3739
}
3840
}
3941

42+
public void loadCodeNotes(GetCodeNotes? codeNotes)
43+
{
44+
this.codeNotes = codeNotes;
45+
if (this.codeNotes != null && this.codeNotes.Success)
46+
{
47+
foreach (var note in this.codeNotes.CodeNotes)
48+
{
49+
this.comments[note.Address] = [
50+
$"`{note.Address}`",
51+
"---",
52+
$"```txt\n{note.Note}\n```",
53+
"---",
54+
$"Author: [{note.User}](https://retroachievements.org/user/{note.User})",
55+
];
56+
}
57+
}
58+
}
59+
60+
public GetCodeNotes? GetCodeNotes()
61+
{
62+
return this.codeNotes;
63+
}
64+
4065
private void Load()
4166
{
4267
for (int i = 0; i < this.functionDefinitions.Length; i++)
@@ -48,6 +73,22 @@ private void Load()
4873
}
4974
if (text != null && text != "")
5075
{
76+
foreach (Match ItemMatch in Regex.Matches(text, @"\/\/\s*#ID\s*=\s*(\d+)"))
77+
{
78+
string gameIDStr = ItemMatch.Groups.Values.ElementAt(1).ToString();
79+
try
80+
{
81+
int gameID = int.Parse(gameIDStr);
82+
if (gameID > 0)
83+
{
84+
this.gameID = gameID;
85+
}
86+
}
87+
catch (FormatException)
88+
{
89+
this.gameID = 0; // reset the game id
90+
}
91+
}
5192
foreach (Match ItemMatch in Regex.Matches(text, @"(\w+)\s*="))
5293
{
5394
string varName = ItemMatch.Groups.Values.ElementAt(1).ToString();
@@ -316,7 +357,7 @@ public string[] GetKeywords()
316357
{
317358
return this.keywords.ToArray();
318359
}
319-
360+
320361
public CompletionItemKind? GetKeywordCompletionItemKind(string keyword)
321362
{
322363
if (this.keywordKinds.ContainsKey(keyword))
@@ -325,5 +366,10 @@ public string[] GetKeywords()
325366
}
326367
return null;
327368
}
369+
370+
public int GetGameID()
371+
{
372+
return this.gameID;
373+
}
328374
}
329375
}

src/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace RAScriptLanguageServer
66
{
7-
class Program
7+
public class Program
88
{
9-
static async Task Main(string[] args)
9+
public static async Task Main(string[] args)
1010
{
1111
var server = await LanguageServer.From(options =>
1212
options

src/TextDocumentSyncHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
88
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
99
using MediatR;
10+
using RASharp;
1011

1112

1213
namespace RAScriptLanguageServer
@@ -21,12 +22,15 @@ internal class TextDocumentSyncHandler : TextDocumentSyncHandlerBase
2122
private readonly ILanguageServerFacade _router;
2223
private readonly BufferManager _bufferManager;
2324
private readonly FunctionDefinitions _functionDefinitions;
25+
private readonly RetroAchievements _retroachievements;
2426

2527
public TextDocumentSyncHandler(ILanguageServerFacade router, BufferManager bufferManager)
2628
{
2729
_router = router;
2830
_bufferManager = bufferManager;
2931
_functionDefinitions = new FunctionDefinitions();
32+
_retroachievements = new RetroAchievements(RetroAchievements.RetroAchievementsHost, "");
33+
_bufferManager.SetRAClient(this._retroachievements);
3034
}
3135

3236
public override TextDocumentAttributes GetTextDocumentAttributes(DocumentUri uri)
@@ -39,7 +43,7 @@ public override Task<Unit> Handle(DidOpenTextDocumentParams request, Cancellatio
3943
var documentPath = request.TextDocument.Uri.ToString();
4044
var text = request.TextDocument.Text;
4145
Parser p = new Parser(_router, _functionDefinitions, text);
42-
_bufferManager.UpdateBuffer(documentPath, new StringBuilder(request.TextDocument.Text), p);
46+
_ = _bufferManager.UpdateBufferAsync(documentPath, new StringBuilder(request.TextDocument.Text), p);
4347
return Unit.Task;
4448
}
4549

@@ -50,7 +54,7 @@ public override Task<Unit> Handle(DidChangeTextDocumentParams request, Cancellat
5054
if (text != null)
5155
{
5256
Parser p = new Parser(_router, _functionDefinitions, text);
53-
_bufferManager.UpdateBuffer(documentPath, new StringBuilder(text), p);
57+
_ = _bufferManager.UpdateBufferAsync(documentPath, new StringBuilder(text), p);
5458
}
5559
return Unit.Task;
5660
}

src/rascript-language-server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.19.9" />
13+
<PackageReference Include="RASharp" Version="0.0.3" />
1314
</ItemGroup>
1415

1516
</Project>

0 commit comments

Comments
 (0)