Skip to content

Commit 54c6cde

Browse files
committed
fix derma panel inheritance
1 parent aca63a2 commit 54c6cde

File tree

8 files changed

+65
-18
lines changed

8 files changed

+65
-18
lines changed

__tests__/utils/string.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { removeNewlines, toLowerCamelCase, putCommentBeforeEachLine } from '../../src/utils/string';
1+
import { removeNewlines, toLowerCamelCase, putCommentBeforeEachLine, safeFileName } from '../../src/utils/string';
22

33
describe('toLowerCamelCase', () => {
44
it('should convert a string to lowerCamelCase', () => {
@@ -37,4 +37,19 @@ describe('putCommentBeforeEachLine', () => {
3737
])('should put a comment before each line', (input, expected, skipFirstLine = false) => {
3838
expect(putCommentBeforeEachLine(input, skipFirstLine)).toBe(expected);
3939
});
40+
});
41+
42+
describe('safeFileName', () => {
43+
it.each([
44+
['hello world','hello world'],
45+
['hello:World', 'hello_World'],
46+
['hello:World', 'hello-World', '-'],
47+
['hello:World', 'helloWorld', ''],
48+
['hello:World', 'hello World', ' '],
49+
])('should make a string "%s" safe ("%s") for use as a file name', (input: string, expected: string, replacement: string|undefined = undefined) => {
50+
if (replacement !== undefined)
51+
expect(safeFileName(input, replacement)).toBe(expected);
52+
else
53+
expect(safeFileName(input)).toBe(expected);
54+
});
4055
});

custom/DMenu.AddOption.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---[CLIENT] Add an option to the DMenu
2+
---
3+
---[(View on wiki)](https://wiki.facepunch.com/gmod/DMenu:AddOption)
4+
---@param name string Name of the option.
5+
---@param func? function Function to execute when this option is clicked.
6+
---@return DMenuOption #Returns the created DMenuOption panel.
7+
function DMenu:AddOption(name, func) end

custom/DMenu.AddPanel.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---[CLIENT] Adds a panel to the DMenu as if it were an option.
2+
---
3+
--- This invokes DScrollPanel:AddItem and will not create a new panel if a class name is passed, unlike Panel:Add.
4+
---
5+
---[(View on wiki)](https://wiki.facepunch.com/gmod/DMenu:AddPanel)
6+
---@generic T : Panel
7+
---@param pnl `T` The panel that you want to add.
8+
function DMenu:AddPanel(pnl) end

custom/DMenu.AddSubMenu.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---[CLIENT] Add a sub menu to the DMenu
2+
---
3+
---[(View on wiki)](https://wiki.facepunch.com/gmod/DMenu:AddSubMenu)
4+
---@param Name string Name of the sub menu.
5+
---@param func? function Function to execute when this sub menu is clicked.
6+
---@return DMenu, DMenuOption #DMenu - The created sub DMenu
7+
---@return DMenu, DMenuOption #DMenuOption - The created DMenuOption
8+
function DMenu:AddSubMenu(Name, func) end

custom/Global.Color.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
---@param r number An integer from `0-255` describing the red value of the color.
66
---@param g number An integer from `0-255` describing the green value of the color.
77
---@param b number An integer from `0-255` describing the blue value of the color.
8-
---@param a number An integer from `0-255` describing the alpha (transparency) of the color.
8+
---@param a? number An integer from `0-255` describing the alpha (transparency) of the color.
99
---@return Color #The created Color. Is returned as a table without the Color metatable, [(See GitHub issue)](https://github.com/Facepunch/garrysmod-issues/issues/2407).
1010
function _G.Color(r, g, b, a) end

custom/Global.DermaMenu.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---[CLIENT AND MENU] Creates a DMenu and closes any current menus.
2+
---
3+
---[(View on wiki)](https://wiki.facepunch.com/gmod/Global.DermaMenu)
4+
---@param keepOpen? boolean If we should keep other DMenus open (`true`) or not (`false`).
5+
---@param parent? Panel The panel to parent the created menu to.
6+
---@return DMenu #The created DMenu.
7+
function _G.DermaMenu(keepOpen, parent) end

src/api-writer/glua-api-writer.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ClassFunction, Enum, Function, HookFunction, LibraryFunction, Panel, PanelFunction, Realm, Struct, WikiPage, isPanel } from '../scrapers/wiki-page-markup-scraper.js';
2-
import { putCommentBeforeEachLine, removeNewlines, toLowerCamelCase } from '../utils/string.js';
2+
import { putCommentBeforeEachLine, removeNewlines, safeFileName, toLowerCamelCase } from '../utils/string.js';
33
import {
44
isClassFunction,
55
isHookFunction,
@@ -62,19 +62,20 @@ export class GluaApiWriter {
6262
}
6363

6464
public addOverride(pageAddress: string, override: string) {
65-
this.pageOverrides.set(pageAddress, override);
65+
this.pageOverrides.set(safeFileName(pageAddress, '.'), override);
6666
}
6767

6868
public writePage(page: WikiPage) {
69-
if (this.pageOverrides.has(page.address)) {
69+
const fileSafeAddress = safeFileName(page.address, '.');
70+
if (this.pageOverrides.has(fileSafeAddress)) {
7071
let api = '';
7172

7273
if (isClassFunction(page))
7374
api += this.writeClass(page.parent);
7475
else if (isLibraryFunction(page))
7576
api += this.writeLibraryGlobal(page);
7677

77-
api += this.pageOverrides.get(page.address);
78+
api += this.pageOverrides.get(fileSafeAddress);
7879

7980
return `${api}\n\n`;
8081
} else if (isClassFunction(page))
@@ -184,18 +185,8 @@ export class GluaApiWriter {
184185
}
185186
};
186187

187-
for (const item of _enum.items) {
188-
const keys = item.key.split(' or ');
189-
190-
if (keys.length > 1) {
191-
console.warn(`Enum item ${item.key} has multiple keys. This is not supported by the Glua API.`);
192-
for (const key of keys) {
193-
writeItem(key, item);
194-
}
195-
} else {
196-
writeItem(item.key, item);
197-
}
198-
}
188+
for (const item of _enum.items)
189+
writeItem(item.key, item);
199190

200191
if (isContainedInTable)
201192
api += '}';

src/utils/string.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ export function putCommentBeforeEachLine(text: string, skipLineOne: boolean = tr
2828

2929
return `--- ${line}`;
3030
}).join('\n');
31+
}
32+
33+
/**
34+
* Makes a string safe for use as a file name
35+
*
36+
* @param str The string to make safe
37+
* @param replacement The string to replace unsafe characters with
38+
* @returns The safe string
39+
*/
40+
export function safeFileName(str: string, replacement: string = '_') {
41+
return str.replace(/[^a-z0-9_\-\. ]/gi, replacement);
3142
}

0 commit comments

Comments
 (0)