Skip to content

Commit d294166

Browse files
committed
feat: 扩展Java集合的代码补全支持
``` 增加ArrayList和HashMap方法的代码补全支持,同时引入List、Set、Queue、Stack和Deque集合的补全功能。补全项包括对各集合特有方法的文档描述,提升Java集合操作的便捷性。 ```
1 parent e7e36d7 commit d294166

File tree

8 files changed

+285
-5
lines changed

8 files changed

+285
-5
lines changed

src/java/arrayList.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export function getArrayListSuggestions(monaco: typeof import('monaco-editor'), range: any) {
32
return [
43
{
@@ -23,13 +22,65 @@ export function getArrayListSuggestions(monaco: typeof import('monaco-editor'),
2322
documentation: 'Returns the element at the specified index.',
2423
range: range
2524
},
25+
{
26+
label: 'set',
27+
kind: monaco.languages.CompletionItemKind.Method,
28+
insertText: 'set(${1:index}, ${2:element});',
29+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
30+
documentation: 'Replaces the element at the specified index with the specified element.',
31+
range: range
32+
},
2633
{
2734
label: 'size',
2835
kind: monaco.languages.CompletionItemKind.Method,
2936
insertText: 'size();',
3037
documentation: 'Returns the number of elements in the ArrayList.',
3138
range: range
39+
},
40+
{
41+
label: 'clear',
42+
kind: monaco.languages.CompletionItemKind.Method,
43+
insertText: 'clear();',
44+
documentation: 'Removes all elements from the ArrayList.',
45+
range: range
46+
},
47+
{
48+
label: 'contains',
49+
kind: monaco.languages.CompletionItemKind.Method,
50+
insertText: 'contains(${1:element});',
51+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
52+
documentation: 'Returns true if this list contains the specified element.',
53+
range: range
54+
},
55+
{
56+
label: 'indexOf',
57+
kind: monaco.languages.CompletionItemKind.Method,
58+
insertText: 'indexOf(${1:element});',
59+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
60+
documentation: 'Returns the index of the first occurrence of the specified element, or -1 if this list does not contain the element.',
61+
range: range
62+
},
63+
{
64+
label: 'lastIndexOf',
65+
kind: monaco.languages.CompletionItemKind.Method,
66+
insertText: 'lastIndexOf(${1:element});',
67+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
68+
documentation: 'Returns the index of the last occurrence of the specified element, or -1 if this list does not contain the element.',
69+
range: range
70+
},
71+
{
72+
label: 'isEmpty',
73+
kind: monaco.languages.CompletionItemKind.Method,
74+
insertText: 'isEmpty();',
75+
documentation: 'Returns true if this list contains no elements.',
76+
range: range
77+
},
78+
{
79+
label: 'toArray',
80+
kind: monaco.languages.CompletionItemKind.Method,
81+
insertText: 'toArray();',
82+
documentation: 'Returns an array containing all of the elements in this list in proper sequence.',
83+
range: range
3284
}
33-
// 其他 ArrayList 方法
3485
];
3586
}

src/java/deque.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export function getDequeSuggestions(monaco: typeof import('monaco-editor'), range: any) {
2+
return [
3+
{
4+
label: 'addFirst',
5+
kind: monaco.languages.CompletionItemKind.Method,
6+
insertText: 'addFirst(${1:element});',
7+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
8+
documentation: 'Inserts the specified element at the front of this deque.',
9+
range: range
10+
},
11+
{
12+
label: 'addLast',
13+
kind: monaco.languages.CompletionItemKind.Method,
14+
insertText: 'addLast(${1:element});',
15+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
16+
documentation: 'Inserts the specified element at the end of this deque.',
17+
range: range
18+
},
19+
{
20+
label: 'removeFirst',
21+
kind: monaco.languages.CompletionItemKind.Method,
22+
insertText: 'removeFirst();',
23+
documentation: 'Removes and returns the first element from this deque.',
24+
range: range
25+
},
26+
{
27+
label: 'removeLast',
28+
kind: monaco.languages.CompletionItemKind.Method,
29+
insertText: 'removeLast();',
30+
documentation: 'Removes and returns the last element from this deque.',
31+
range: range
32+
},
33+
{
34+
label: 'size',
35+
kind: monaco.languages.CompletionItemKind.Method,
36+
insertText: 'size();',
37+
documentation: 'Returns the number of elements in this deque.',
38+
range: range
39+
}
40+
];
41+
}

src/java/hashMap.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export function getHashMapSuggestions(monaco: typeof import('monaco-editor'), range: any) {
32
return [
43
{
@@ -13,7 +12,7 @@ export function getHashMapSuggestions(monaco: typeof import('monaco-editor'), ra
1312
label: 'get',
1413
kind: monaco.languages.CompletionItemKind.Method,
1514
insertText: 'get(${1:key});',
16-
documentation: 'Returns the value to which the specified key is mapped.',
15+
documentation: 'Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.',
1716
range: range
1817
},
1918
{
@@ -30,13 +29,48 @@ export function getHashMapSuggestions(monaco: typeof import('monaco-editor'), ra
3029
documentation: 'Returns true if this map contains a mapping for the specified key.',
3130
range: range
3231
},
32+
{
33+
label: 'containsValue',
34+
kind: monaco.languages.CompletionItemKind.Method,
35+
insertText: 'containsValue(${1:value});',
36+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
37+
documentation: 'Returns true if this map maps one or more keys to the specified value.',
38+
range: range
39+
},
3340
{
3441
label: 'keySet',
3542
kind: monaco.languages.CompletionItemKind.Method,
3643
insertText: 'keySet();',
3744
documentation: 'Returns a Set view of the keys contained in this map.',
3845
range: range
3946
},
40-
// 可以继续添加更多方法提示
47+
{
48+
label: 'size',
49+
kind: monaco.languages.CompletionItemKind.Method,
50+
insertText: 'size();',
51+
documentation: 'Returns the number of key-value mappings in this map.',
52+
range: range
53+
},
54+
{
55+
label: 'clear',
56+
kind: monaco.languages.CompletionItemKind.Method,
57+
insertText: 'clear();',
58+
documentation: 'Removes all of the mappings from this map. The map will be empty after this call.',
59+
range: range
60+
},
61+
{
62+
label: 'values',
63+
kind: monaco.languages.CompletionItemKind.Method,
64+
insertText: 'values();',
65+
documentation: 'Returns a Collection view of the values contained in this map.',
66+
range: range
67+
},
68+
{
69+
label: 'entrySet',
70+
kind: monaco.languages.CompletionItemKind.Method,
71+
insertText: 'entrySet();',
72+
documentation: 'Returns a Set view of the mappings contained in this map.',
73+
range: range
74+
}
4175
];
4276
}

src/java/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { languages } from 'monaco-editor';
22
import { getArrayListSuggestions } from './arrayList';
33
import { getHashMapSuggestions } from './hashMap';
4+
import { getListSuggestions } from './list';
5+
import { getSetSuggestions } from './set';
6+
import { getQueueSuggestions } from './queue';
7+
import { getStackSuggestions } from './stack';
8+
import { getDequeSuggestions } from './deque';
9+
410
import { getJavaRange, extractJavaVariables } from './utils';
511
import { getGeneralSuggestions } from './getGeneralSuggestions';
612

@@ -49,9 +55,25 @@ export function initJavaCompletion(monaco: typeof import('monaco-editor')) {
4955
case 'HashMap':
5056
methodSuggestions = getHashMapSuggestions(monaco, range);
5157
break;
58+
case 'List':
59+
methodSuggestions = getListSuggestions(monaco, range);
60+
break;
61+
case 'Set':
62+
methodSuggestions = getSetSuggestions(monaco, range);
63+
break;
64+
case 'Queue':
65+
methodSuggestions = getQueueSuggestions(monaco, range);
66+
break;
67+
case 'Stack':
68+
methodSuggestions = getStackSuggestions(monaco, range);
69+
break;
70+
case 'Deque':
71+
methodSuggestions = getDequeSuggestions(monaco, range);
72+
break;
5273
default:
5374
break;
5475
}
76+
5577
}
5678
}
5779

src/java/list.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function getListSuggestions(monaco: typeof import('monaco-editor'), range: any) {
2+
return [
3+
{
4+
label: 'add',
5+
kind: monaco.languages.CompletionItemKind.Method,
6+
insertText: 'add(${1:element});',
7+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
8+
documentation: 'Appends the specified element to the end of this list.',
9+
range: range
10+
},
11+
{
12+
label: 'get',
13+
kind: monaco.languages.CompletionItemKind.Method,
14+
insertText: 'get(${1:index});',
15+
documentation: 'Returns the element at the specified position in this list.',
16+
range: range
17+
},
18+
{
19+
label: 'remove',
20+
kind: monaco.languages.CompletionItemKind.Method,
21+
insertText: 'remove(${1:index});',
22+
documentation: 'Removes the element at the specified position in this list.',
23+
range: range
24+
},
25+
{
26+
label: 'size',
27+
kind: monaco.languages.CompletionItemKind.Method,
28+
insertText: 'size();',
29+
documentation: 'Returns the number of elements in this list.',
30+
range: range
31+
}
32+
];
33+
}

src/java/queue.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function getQueueSuggestions(monaco: typeof import('monaco-editor'), range: any) {
2+
return [
3+
{
4+
label: 'offer',
5+
kind: monaco.languages.CompletionItemKind.Method,
6+
insertText: 'offer(${1:element});',
7+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
8+
documentation: 'Inserts the specified element into this queue.',
9+
range: range
10+
},
11+
{
12+
label: 'poll',
13+
kind: monaco.languages.CompletionItemKind.Method,
14+
insertText: 'poll();',
15+
documentation: 'Retrieves and removes the head of this queue, or returns null if this queue is empty.',
16+
range: range
17+
},
18+
{
19+
label: 'peek',
20+
kind: monaco.languages.CompletionItemKind.Method,
21+
insertText: 'peek();',
22+
documentation: 'Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.',
23+
range: range
24+
},
25+
{
26+
label: 'size',
27+
kind: monaco.languages.CompletionItemKind.Method,
28+
insertText: 'size();',
29+
documentation: 'Returns the number of elements in this queue.',
30+
range: range
31+
}
32+
];
33+
}

src/java/set.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function getSetSuggestions(monaco: typeof import('monaco-editor'), range: any) {
2+
return [
3+
{
4+
label: 'add',
5+
kind: monaco.languages.CompletionItemKind.Method,
6+
insertText: 'add(${1:element});',
7+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
8+
documentation: 'Adds the specified element to this set if it is not already present.',
9+
range: range
10+
},
11+
{
12+
label: 'contains',
13+
kind: monaco.languages.CompletionItemKind.Method,
14+
insertText: 'contains(${1:element});',
15+
documentation: 'Returns true if this set contains the specified element.',
16+
range: range
17+
},
18+
{
19+
label: 'remove',
20+
kind: monaco.languages.CompletionItemKind.Method,
21+
insertText: 'remove(${1:element});',
22+
documentation: 'Removes the specified element from this set if it is present.',
23+
range: range
24+
},
25+
{
26+
label: 'size',
27+
kind: monaco.languages.CompletionItemKind.Method,
28+
insertText: 'size();',
29+
documentation: 'Returns the number of elements in this set.',
30+
range: range
31+
}
32+
];
33+
}

src/java/stack.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function getStackSuggestions(monaco: typeof import('monaco-editor'), range: any) {
2+
return [
3+
{
4+
label: 'push',
5+
kind: monaco.languages.CompletionItemKind.Method,
6+
insertText: 'push(${1:element});',
7+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
8+
documentation: 'Pushes an item onto the top of this stack.',
9+
range: range
10+
},
11+
{
12+
label: 'pop',
13+
kind: monaco.languages.CompletionItemKind.Method,
14+
insertText: 'pop();',
15+
documentation: 'Removes the object at the top of this stack and returns that object as the value of this function.',
16+
range: range
17+
},
18+
{
19+
label: 'peek',
20+
kind: monaco.languages.CompletionItemKind.Method,
21+
insertText: 'peek();',
22+
documentation: 'Looks at the object at the top of this stack without removing it from the stack.',
23+
range: range
24+
},
25+
{
26+
label: 'size',
27+
kind: monaco.languages.CompletionItemKind.Method,
28+
insertText: 'size();',
29+
documentation: 'Returns the number of elements in this stack.',
30+
range: range
31+
}
32+
];
33+
}

0 commit comments

Comments
 (0)