Skip to content

Commit 4b69bc6

Browse files
Merge pull request #5700 from kwvanderlinde/bugfix/5696-macro-location
Fix macro callable locations
2 parents 4542d30 + ef15f2c commit 4b69bc6

File tree

3 files changed

+76
-98
lines changed

3 files changed

+76
-98
lines changed

src/main/java/net/rptools/maptool/client/macro/MacroLocation.java

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package net.rptools.maptool.client.macro;
1616

1717
import java.net.URI;
18-
import java.net.URISyntaxException;
1918
import javax.annotation.Nonnull;
2019
import javax.annotation.Nullable;
2120
import net.rptools.maptool.model.Token;
@@ -52,12 +51,12 @@ public class MacroLocation {
5251
/** The location of the macro. */
5352
@Nonnull private final String location;
5453

54+
/** the location of the macro that can be called from macro code using the '@' syntax */
55+
@Nonnull private final String callableLocation;
56+
5557
/** The URI of the macro, if applicable. */
5658
@Nullable private final URI uri;
5759

58-
/** The token associated with this macro, if applicable. */
59-
@Nullable private final Token token;
60-
6160
/** MacroLocationFactory used to create locations during parsing. */
6261
private static final MacroLocationFactory factory = MacroLocationFactory.getInstance();
6362

@@ -68,19 +67,18 @@ public class MacroLocation {
6867
* @param source the source of the macro.
6968
* @param location the location of the macro.
7069
* @param uri the URI of the macro, if applicable.
71-
* @param token the associated token, if applicable.
7270
*/
7371
MacroLocation(
7472
@Nonnull String name,
7573
@Nonnull MacroSource source,
7674
@Nonnull String location,
77-
@Nullable URI uri,
78-
@Nullable Token token) {
75+
@Nonnull String callableLocation,
76+
@Nullable URI uri) {
7977
this.name = name;
8078
this.source = source;
8179
this.location = location;
80+
this.callableLocation = callableLocation;
8281
this.uri = uri;
83-
this.token = token;
8482
}
8583

8684
/**
@@ -90,8 +88,12 @@ public class MacroLocation {
9088
* @param source the source of the macro.
9189
* @param location the location of the macro.
9290
*/
93-
MacroLocation(@Nonnull String name, @Nonnull MacroSource source, @Nonnull String location) {
94-
this(name, source, location, null, null);
91+
MacroLocation(
92+
@Nonnull String name,
93+
@Nonnull MacroSource source,
94+
@Nonnull String location,
95+
@Nonnull String callableLocation) {
96+
this(name, source, location, callableLocation, null);
9597
}
9698

9799
/** Enumeration to represent the source of the macro. */
@@ -173,42 +175,29 @@ public static MacroLocation parseMacroName(
173175
String qMacroNameLower = qMacroName.toLowerCase();
174176

175177
if (qMacroNameLower.contains("@campaign")) {
176-
return new MacroLocation(
177-
qMacroName.substring(0, qMacroName.indexOf("@")),
178-
MacroSource.campaign,
179-
MacroSource.campaign.getSourceName());
178+
return factory.createCampaignLocation(qMacroName.substring(0, qMacroName.indexOf("@")));
180179
}
181180

182181
if (qMacroNameLower.contains("@gm")) {
183-
return new MacroLocation(
184-
qMacroName.substring(0, qMacroName.indexOf("@")),
185-
MacroSource.gm,
186-
MacroSource.gm.getSourceName());
182+
return factory.createGmLocation(qMacroName.substring(0, qMacroName.indexOf("@")));
187183
}
188184

189185
if (qMacroNameLower.contains("@global")) {
190-
return new MacroLocation(
191-
qMacroName.substring(0, qMacroName.indexOf("@")),
192-
MacroSource.global,
193-
MacroSource.global.getSourceName());
186+
return factory.createGlobalLocation(qMacroName.substring(0, qMacroName.indexOf("@")));
194187
}
195188

196189
if (qMacroNameLower.contains("@token")) {
197190
if (token == null) {
198191
return factory.createUnknownLocation(qMacroName);
199192
}
200-
return new MacroLocation(
201-
qMacroName.substring(0, qMacroName.indexOf("@")),
202-
MacroSource.token,
203-
token.getName(),
204-
null,
205-
token);
193+
return factory.createTokenLocation(qMacroName.substring(0, qMacroName.indexOf("@")), token);
206194
}
207195

208196
if (qMacroNameLower.contains("@lib:")) {
209197
String macroName = qMacroName.substring(0, qMacroName.indexOf("@"));
210-
String namespace = qMacroName.replaceFirst("^[^@]*@(?i)lib:", "");
211-
return new MacroLocation(macroName, MacroSource.library, namespace, null, token);
198+
String location = qMacroName.substring(qMacroName.indexOf("@") + 1);
199+
String namespace = location.replaceFirst("(?i)lib:", "");
200+
return new MacroLocation(macroName, MacroSource.library, namespace, location);
212201
}
213202

214203
if (qMacroNameLower.contains("@this")) {
@@ -222,30 +211,16 @@ public static MacroLocation parseMacroName(
222211
return factory.createUnknownLocation(qMacroName);
223212
}
224213
}
225-
return new MacroLocation(name, cfrom.getSource(), cfrom.getLocation(), null, token);
214+
return new MacroLocation(
215+
name, cfrom.getSource(), cfrom.getLocation(), cfrom.getCallableLocation());
226216
}
227217

228218
// If none of the above then assume it is a URI
229-
URI uri;
230-
try {
231-
uri = new URI(qMacroName);
232-
} catch (URISyntaxException e) {
233-
return factory.createUnknownLocation(qMacroName);
234-
}
235-
236-
if (uri.getHost() == null) {
237-
if (calledFrom != null && calledFrom.getSource() == MacroSource.uri) {
238-
uri = calledFrom.getUri().resolve(uri);
239-
} else {
240-
return factory.createUnknownLocation(qMacroName);
241-
}
242-
}
243-
244-
if (uri.getScheme() == null || !uri.getScheme().toLowerCase().equals("lib")) {
245-
return factory.createUnknownLocation(qMacroName);
246-
}
247-
248-
return new MacroLocation(uri.getPath().substring(1), MacroSource.uri, uri.getHost(), uri, null);
219+
return factory.createUriLocation(
220+
qMacroName,
221+
calledFrom != null && calledFrom.getSource() == MacroSource.uri
222+
? calledFrom.getUri()
223+
: null);
249224
}
250225

251226
/**
@@ -284,29 +259,14 @@ public URI getUri() {
284259
return uri;
285260
}
286261

287-
/**
288-
* Returns the token associated with this macro, if applicable.
289-
*
290-
* @return the token associated with this macro, or null if not applicable.
291-
*/
292-
public Token getToken() {
293-
return token;
294-
}
295-
296262
/**
297263
* Returns the location of the macro that can be called from macro code using the '@' syntax.
298264
*
299265
* @return the location of the macro that can be called from other macros.
300266
*/
301267
@Nonnull
302268
public String getCallableLocation() {
303-
if (source == MacroSource.token) {
304-
return "Token:" + location;
305-
} else if (source == MacroSource.library) {
306-
return token != null ? token.getName() : location;
307-
} else {
308-
return location;
309-
}
269+
return callableLocation;
310270
}
311271

312272
@Override
@@ -322,10 +282,6 @@ public String toString() {
322282
sb.append(", uri='");
323283
sb.append(uri);
324284
}
325-
if (token != null) {
326-
sb.append(", token='");
327-
sb.append(token.getName());
328-
}
329285
sb.append("'}");
330286

331287
return sb.toString();

src/main/java/net/rptools/maptool/client/macro/MacroLocationFactory.java

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MacroLocationFactory {
3131
private MacroLocationFactory() {}
3232

3333
/** The singleton instance of the factory. */
34-
private static MacroLocationFactory instance = new MacroLocationFactory();
34+
private static final MacroLocationFactory instance = new MacroLocationFactory();
3535

3636
/**
3737
* Returns the singleton instance of the factory.
@@ -49,7 +49,7 @@ public static MacroLocationFactory getInstance() {
4949
* @return a new {@link MacroLocation} object for an unknown location.
5050
*/
5151
public MacroLocation createUnknownLocation(@Nonnull String name) {
52-
return new MacroLocation(name, MacroSource.unknown, "");
52+
return new MacroLocation(name, MacroSource.unknown, "", "");
5353
}
5454

5555
/**
@@ -59,7 +59,11 @@ public MacroLocation createUnknownLocation(@Nonnull String name) {
5959
* @return a new {@link MacroLocation} object for a global Panel.
6060
*/
6161
public MacroLocation createGlobalLocation(@Nonnull String name) {
62-
return new MacroLocation(name, MacroSource.global, MacroSource.global.getSourceName());
62+
return new MacroLocation(
63+
name,
64+
MacroSource.global,
65+
MacroSource.global.getSourceName(),
66+
MacroSource.global.getSourceName());
6367
}
6468

6569
/**
@@ -69,7 +73,11 @@ public MacroLocation createGlobalLocation(@Nonnull String name) {
6973
* @return a new {@link MacroLocation} object for a campaign Panel.
7074
*/
7175
public MacroLocation createCampaignLocation(@Nonnull String name) {
72-
return new MacroLocation(name, MacroSource.campaign, MacroSource.campaign.getSourceName());
76+
return new MacroLocation(
77+
name,
78+
MacroSource.campaign,
79+
MacroSource.campaign.getSourceName(),
80+
MacroSource.campaign.getSourceName());
7381
}
7482

7583
/**
@@ -80,7 +88,7 @@ public MacroLocation createCampaignLocation(@Nonnull String name) {
8088
* @return a new {@link MacroLocation} object for a token.
8189
*/
8290
public MacroLocation createTokenLocation(@Nonnull String name, @Nonnull Token token) {
83-
return new MacroLocation(name, MacroSource.token, token.getName(), null, token);
91+
return new MacroLocation(name, MacroSource.token, token.getName(), "Token:" + token.getName());
8492
}
8593

8694
/**
@@ -92,7 +100,7 @@ public MacroLocation createTokenLocation(@Nonnull String name, @Nonnull Token to
92100
*/
93101
public MacroLocation createLibTokenLocation(@Nonnull String name, @Nonnull Token libToken) {
94102
return new MacroLocation(
95-
name, MacroSource.library, libToken.getName().substring(4), null, libToken);
103+
name, MacroSource.library, libToken.getName().substring(4), libToken.getName());
96104
}
97105

98106
/**
@@ -102,18 +110,22 @@ public MacroLocation createLibTokenLocation(@Nonnull String name, @Nonnull Token
102110
* @return a new {@link MacroLocation} object for a GM Panel.
103111
*/
104112
public MacroLocation createGmLocation(@Nonnull String name) {
105-
return new MacroLocation(name, MacroSource.gm, MacroSource.gm.getSourceName());
113+
return new MacroLocation(
114+
name, MacroSource.gm, MacroSource.gm.getSourceName(), MacroSource.gm.getSourceName());
106115
}
107116

108117
/**
109118
* Creates a new {@link MacroLocation} object for a library location.
110119
*
111-
* @param name the name of the macro.
120+
* @param functionName the name of the function.
112121
* @return a new {@link MacroLocation} object for a library location.
113122
*/
114123
public MacroLocation createExecFunctionLocation(@Nonnull String functionName) {
115124
return new MacroLocation(
116-
MacroSource.execFunction.getSourceName(), MacroSource.execFunction, functionName);
125+
MacroSource.execFunction.getSourceName(),
126+
MacroSource.execFunction,
127+
functionName,
128+
functionName);
117129
}
118130

119131
/**
@@ -126,6 +138,7 @@ public MacroLocation createMacroLinkLocation(@Nonnull String name) {
126138
return new MacroLocation(
127139
MacroSource.macroLink.getSourceName(),
128140
MacroSource.macroLink,
141+
MacroSource.macroLink.getSourceName(),
129142
MacroSource.macroLink.getSourceName());
130143
}
131144

@@ -136,13 +149,14 @@ public MacroLocation createMacroLinkLocation(@Nonnull String name) {
136149
* @return a new {@link MacroLocation} object for an event.
137150
*/
138151
public MacroLocation createEventLocation(@Nonnull String name) {
139-
return new MacroLocation(MacroSource.event.getSourceName(), MacroSource.event, name);
152+
return new MacroLocation(MacroSource.event.getSourceName(), MacroSource.event, name, name);
140153
}
141154

142155
public MacroLocation createSentryIoLoggingLocation() {
143156
return new MacroLocation(
144157
MacroSource.sentryIoLogging.getSourceName(),
145158
MacroSource.sentryIoLogging,
159+
MacroSource.sentryIoLogging.getSourceName(),
146160
MacroSource.sentryIoLogging.getSourceName());
147161
}
148162

@@ -154,29 +168,40 @@ public MacroLocation createSentryIoLoggingLocation() {
154168
* @return a new {@link MacroLocation} object for a URI location.
155169
*/
156170
public MacroLocation createUriLocation(@Nonnull String name, @Nullable URI calledFrom) {
171+
URI uri;
157172
try {
158-
var uri = new URI(name);
159-
if (uri.getScheme() == null) {
160-
if (calledFrom == null) {
161-
return createUnknownLocation(name);
162-
}
163-
uri = calledFrom.resolve(uri);
164-
}
165-
return new MacroLocation(uri.getPath(), MacroSource.uri, uri.getHost(), uri, null);
173+
uri = new URI(name);
166174
} catch (URISyntaxException e) {
167175
return createUnknownLocation(name);
168176
}
177+
178+
if (uri.getHost() == null) {
179+
if (calledFrom == null) {
180+
return createUnknownLocation(name);
181+
}
182+
uri = calledFrom.resolve(uri);
183+
}
184+
185+
var scheme = uri.getScheme();
186+
if (scheme == null || !scheme.equalsIgnoreCase("lib")) {
187+
return createUnknownLocation(name);
188+
}
189+
190+
return new MacroLocation(
191+
uri.getPath().substring(1), MacroSource.uri, uri.getHost(), uri.getHost(), uri);
169192
}
170193

171194
/**
172195
* Creates a new {@link MacroLocation} object for the chat box.
173196
*
174-
* @param token the token associated with the chat box.
175197
* @return a new {@link MacroLocation} object for a the chat box.
176198
*/
177199
public MacroLocation createChatLocation() {
178200
return new MacroLocation(
179-
MacroSource.chat.getSourceName(), MacroSource.chat, MacroSource.chat.getSourceName());
201+
MacroSource.chat.getSourceName(),
202+
MacroSource.chat,
203+
MacroSource.chat.getSourceName(),
204+
MacroSource.chat.getSourceName());
180205
}
181206

182207
/**
@@ -186,11 +211,8 @@ public MacroLocation createChatLocation() {
186211
* @return a new {@link MacroLocation} object for a tooltip.
187212
*/
188213
public MacroLocation createToolTipLocation(@Nullable Token token) {
214+
var location = token != null ? token.getName() : "";
189215
return new MacroLocation(
190-
MacroSource.tooltip.getSourceName(),
191-
MacroSource.tooltip,
192-
token != null ? token.getName() : "",
193-
null,
194-
token);
216+
MacroSource.tooltip.getSourceName(), MacroSource.tooltip, location, location);
195217
}
196218
}

src/test/java/net/rptools/maptool/client/macro/MacroLocationFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void testCreateGmLocation() {
8989
void testCreateUriLocation() throws URISyntaxException {
9090
MacroLocation location =
9191
factory.createUriLocation("lib://test/macro/m2", new URI("lib://lib-test.net/macros/m1"));
92-
assertEquals("/macro/m2", location.getName());
92+
assertEquals("macro/m2", location.getName());
9393
assertEquals(MacroLocation.MacroSource.uri, location.getSource());
9494
assertEquals("test", location.getLocation());
9595
assertNotNull(location.getUri());
@@ -110,7 +110,7 @@ void testCreateUriLocationInvalid() throws URISyntaxException {
110110
void testCreateRelativeUriLocationRelative() throws URISyntaxException {
111111
MacroLocation location =
112112
factory.createUriLocation("relative/path", new URI("lib://test-lib.net/macros/m1"));
113-
assertEquals("/macros/relative/path", location.getName());
113+
assertEquals("macros/relative/path", location.getName());
114114
assertEquals(MacroLocation.MacroSource.uri, location.getSource());
115115
assertEquals("test-lib.net", location.getLocation());
116116
assertNotNull(location.getUri());

0 commit comments

Comments
 (0)