Skip to content

Commit 813a02d

Browse files
committed
simple csharp autocomplete
1 parent 3d44ed1 commit 813a02d

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

src/uLearn.Web/App_Start/BundleConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static void RegisterBundles(BundleCollection bundles)
2222
"~/codemirror/lib/codemirror.js",
2323
"~/codemirror/mode/clike/clike.js",
2424
"~/codemirror/addon/hint/show-hint.js",
25-
"~/codemirror/addon/hint/anyword-hint.js"
25+
//"~/codemirror/addon/hint/anyword-hint.js",
26+
"~/codemirror/addon/hint/csharp-hint.js"
2627
));
2728

2829
bundles.Add(new ScriptBundle("~/bootstrap").Include(

src/uLearn.Web/Scripts/_references.js

132 Bytes
Binary file not shown.

src/uLearn.Web/Views/Course/Slide.cshtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
@section scripts{
5353
<script src="~/Scripts/navigate.js"></script>
5454
<script>
55+
CodeMirror.commands.autocomplete = function (cm) {
56+
cm.showHint({ hint: CodeMirror.hint.csharp });
57+
}
58+
5559
function codeMirrorClass(c, theme) {
5660
var codes = document.getElementsByClassName(c);
5761
for (var i = 0; i < codes.length; i++)
@@ -63,6 +67,7 @@
6367
indentWithTabs: true,
6468
tabSize: 4,
6569
indentUnit: 4,
70+
extraKeys: { "Ctrl-Space": "autocomplete" },
6671
});
6772
}
6873
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
(function (mod) {
2+
if (typeof exports == "object" && typeof module == "object") // CommonJS
3+
mod(require("../../lib/codemirror"));
4+
else if (typeof define == "function" && define.amd) // AMD
5+
define(["../../lib/codemirror"], mod);
6+
else // Plain browser env
7+
mod(CodeMirror);
8+
})(function (CodeMirror) {
9+
"use strict";
10+
11+
function forEach(arr, f) {
12+
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
13+
}
14+
15+
function arrayContains(arr, item) {
16+
if (!Array.prototype.indexOf) {
17+
var i = arr.length;
18+
while (i--) {
19+
if (arr[i] === item) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
return arr.indexOf(item) != -1;
26+
}
27+
28+
function csharpHint(editor) {
29+
var cur = editor.getCursor(), token = editor.getTokenAt(cur), tprop = token;
30+
var from = CodeMirror.Pos(cur.line, token.start);
31+
var completionList;
32+
if (token.string === ".") {
33+
from = CodeMirror.Pos(cur.line, token.start + 1);
34+
completionList = getCompletions("", true);
35+
}
36+
else if (!/^[\w@_]*$/.test(token.string)) {
37+
from = cur;
38+
completionList = getCompletions("", false);
39+
}
40+
else
41+
completionList = getCompletions(token.string, false);
42+
43+
completionList = completionList.sort();
44+
45+
return {
46+
list: completionList,
47+
from: from,
48+
to: CodeMirror.Pos(cur.line, token.end)
49+
};
50+
}
51+
var keywords = Object.keys(CodeMirror.resolveMode("text/x-csharp").keywords);
52+
var methods = (""
53+
+ " WriteLine Write"
54+
+ " Format Substring Length Parse TryParse ToString Split Join"
55+
+ " Where Select SelectMany ToArray ToList ToDictionary ToLookup Join Zip Aggregate GroupBy OrderBy OrderByDescending ThenBy ThenByDescending"
56+
+ " "
57+
).split(" ");
58+
var types = ("Console CultureInfo Enumerable "
59+
+ " "
60+
).split(" ");
61+
62+
function getCompletions(start, afterDot) {
63+
var found = [];
64+
console.log("|" + start + "|");
65+
function maybeAdd(str) {
66+
if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
67+
}
68+
if (afterDot) {
69+
forEach(methods, maybeAdd);
70+
}
71+
else {
72+
forEach(types, maybeAdd);
73+
forEach(methods, maybeAdd);
74+
forEach(keywords, maybeAdd);
75+
}
76+
return found;
77+
}
78+
79+
CodeMirror.registerHelper("hint", "csharp", csharpHint);
80+
});

src/uLearn.Web/uLearn.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
<Content Include="codemirror\addon\hint\css-hint.js" />
254254
<Content Include="codemirror\addon\hint\html-hint.js" />
255255
<Content Include="codemirror\addon\hint\javascript-hint.js" />
256+
<Content Include="codemirror\addon\hint\csharp-hint.js" />
256257
<Content Include="codemirror\addon\hint\python-hint.js" />
257258
<Content Include="codemirror\addon\hint\show-hint.css" />
258259
<Content Include="codemirror\addon\hint\show-hint.js" />

0 commit comments

Comments
 (0)