2[
WorkbenchPluginAttribute(name:
"Copy Class and Method Name", description:
"Copy class and method name based on the current line.\nWhen it is inside of a method, the format is 'MyClass.MyMethod()', otherwise the result is just 'MyClass'", shortcut:
"Ctrl+Shift+C", wbModules: {
"ScriptEditor" }, awesomeFontCode: 0xF0C5)]
3class SCR_CopyClassAndMethodPlugin : WorkbenchPlugin
8 ScriptEditor scriptEditor = Workbench.GetModule(ScriptEditor);
10 string className, methodName;
11 if (!GetCursorClassAndMethodNames(scriptEditor, className, methodName))
13 Print(
"Nothing copied to clipboard, the current line is not inside of a class.",
LogLevel.DEBUG);
19 result =
string.Format(
"%1.%2()", className, methodName);
23 System.ExportToClipboard(result);
24 Print(
string.Format(
"Copied to clipboard: \"%1\"", result),
LogLevel.DEBUG);
33 static bool GetCursorClassAndMethodNames(notnull ScriptEditor scriptEditor, out
string className, out
string methodName)
36 array<string> lines = {};
37 for (
int lineId, lineCount = scriptEditor.GetCurrentLine() + 1; lineId < lineCount; ++lineId)
39 scriptEditor.GetLineText(line, lineId);
43 return GetLineClassAndMethodNames(lines, scriptEditor.GetCurrentLine(), className, methodName);
53 static bool GetLineClassAndMethodNames(notnull array<string> lines,
int cursorLineId, out
string className, out
string methodName)
55 const string bracketOpen =
"{";
56 const string bracketClose =
"}";
57 const string commentOpen =
"/" +
"*";
58 const string commentClose =
"*" +
"/";
61 array<int> scopes = {};
63 int linesCount = cursorLineId;
65 foreach (
int lineId,
string line : lines)
67 if (lineId > linesCount)
75 if (line.StartsWith(
"["))
78 if (line.StartsWith(
"/" +
"/"))
81 if (line.StartsWith(bracketOpen))
83 scopes.Insert(lineId - 1);
85 else if (line.StartsWith(bracketClose))
87 if (!scopes.IsEmpty())
88 scopes.Resize(scopes.Count() - 1);
90 else if (line.StartsWith(commentOpen))
99 array<string> tokens = {};
104 if (line.StartsWith(commentClose) || line.EndsWith(commentClose))
109 if (scopes.IsEmpty())
110 scopes.Insert(cursorLineId);
112 string line = lines[scopes[0]];
116 array<string> lineArray = {};
118 lineArray.RemoveItemOrdered(
"modded");
119 lineArray.RemoveItemOrdered(
"sealed");
120 if (lineArray.Count() < 2)
123 className = lineArray[1];
124 className.Replace(
":",
string.Empty);
125 className.TrimInPlace();
127 if (scopes.Count() == 1)
128 scopes.Insert(cursorLineId);
130 if (!lines.IsIndexValid(scopes[1]))
132 methodName =
string.Empty;
136 if (lines[scopes[1]].Trim().StartsWith(bracketOpen))
137 scopes[1] = scopes[1] - 1;
140 lines[scopes[1]].Split(
"(", lineArray,
false);
141 if (lineArray.Count() >= 2)
143 methodName = lineArray[0];
145 if (lineArray.Count() > 1)
146 methodName = lineArray[lineArray.Count() - 1];
148 methodName =
string.Empty;
static bool StartsWithAny(string input, notnull array< string > lineStarts)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.