1 module workspaced.com.snippets.control_flow; 2 3 import workspaced.api; 4 import workspaced.com.snippets; 5 6 import std.algorithm; 7 import std.conv; 8 import std.string; 9 10 class ControlFlowSnippetProvider : SnippetProvider 11 { 12 Future!(Snippet[]) provideSnippets(scope const WorkspaceD.Instance instance, 13 scope const(char)[] file, scope const(char)[] code, int position, const SnippetInfo info) 14 { 15 Snippet[] res; 16 17 SnippetLevel lastBreakable = info.findInLocalScope(SnippetLevel.loop, SnippetLevel.switch_); 18 19 if (lastBreakable != SnippetLevel.init) 20 { 21 bool isSwitch = lastBreakable == SnippetLevel.switch_; 22 23 { 24 Snippet snp; 25 snp.providerId = typeid(this).name; 26 snp.id = snp.title = snp.shortcut = "break"; 27 snp.plain = snp.snippet = "break;"; 28 snp.documentation = isSwitch 29 ? "break out of the current switch" 30 : "break out of this loop"; 31 snp.resolved = true; 32 snp.unformatted = true; 33 res ~= snp; 34 } 35 36 if (isSwitch) 37 { 38 { 39 Snippet snp; 40 snp.providerId = typeid(this).name; 41 snp.id = snp.title = snp.shortcut = "goto case"; 42 snp.plain = snp.snippet = "goto case;"; 43 snp.documentation = "Explicit fall-through into the next case or to case with explicitly given value.\n\nReference: https://dlang.org/spec/statement.html#goto-statement"; 44 snp.resolved = true; 45 snp.unformatted = true; 46 res ~= snp; 47 } 48 { 49 Snippet snp; 50 snp.providerId = typeid(this).name; 51 snp.id = snp.title = snp.shortcut = "goto default"; 52 snp.plain = snp.snippet = "goto default;"; 53 snp.documentation = "Go to default case.\n\nReference: https://dlang.org/spec/statement.html#goto-statement"; 54 snp.resolved = true; 55 snp.unformatted = true; 56 res ~= snp; 57 } 58 { 59 Snippet snp; 60 snp.providerId = typeid(this).name; 61 snp.id = snp.title = snp.shortcut = "case"; 62 snp.snippet = "case ${1}:\n\t$0\n\tbreak;"; 63 snp.documentation = "Defines a case in the current switch-case.\n\nReference: https://dlang.org/spec/statement.html#switch-statement"; 64 snp.resolved = true; 65 snp.unformatted = true; 66 res ~= snp; 67 } 68 { 69 Snippet snp; 70 snp.providerId = typeid(this).name; 71 snp.id = snp.title = snp.shortcut = "case range"; 72 snp.snippet = "case ${1}: .. case ${2}:\n\t$0\n\tbreak;"; 73 snp.documentation = "Defines a range of cases in the current switch-case, with inclusive start and end.\n\nReference: https://dlang.org/spec/statement.html#switch-statement"; 74 snp.resolved = true; 75 snp.unformatted = true; 76 res ~= snp; 77 } 78 { 79 Snippet snp; 80 snp.providerId = typeid(this).name; 81 snp.id = snp.title = snp.shortcut = "default"; 82 snp.snippet = "case:\n\t$0\n\tbreak;"; 83 snp.documentation = "Defines the default case in the current switch-case.\n\nReference: https://dlang.org/spec/statement.html#switch-statement"; 84 snp.resolved = true; 85 snp.unformatted = true; 86 res ~= snp; 87 } 88 } 89 else 90 { 91 { 92 Snippet snp; 93 snp.providerId = typeid(this).name; 94 snp.id = snp.title = snp.shortcut = "continue"; 95 snp.plain = snp.snippet = "continue;"; 96 snp.documentation = "Continue with next iteration"; 97 snp.resolved = true; 98 snp.unformatted = true; 99 res ~= snp; 100 } 101 } 102 } 103 104 return typeof(return).fromResult(res.length ? res : null); 105 } 106 107 Future!Snippet resolveSnippet(scope const WorkspaceD.Instance instance, 108 scope const(char)[] file, scope const(char)[] code, int position, 109 const SnippetInfo info, Snippet snippet) 110 { 111 return typeof(return).fromResult(snippet); 112 } 113 }