Skip to content

Commit 1e340de

Browse files
committed
Stub autocompletion
1 parent 933a006 commit 1e340de

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2003-2024 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.gde.glsl.highlighter.editor;
33+
34+
import javax.swing.text.BadLocationException;
35+
import javax.swing.text.Document;
36+
import javax.swing.text.Element;
37+
import javax.swing.text.JTextComponent;
38+
import javax.swing.text.StyledDocument;
39+
import org.netbeans.api.editor.mimelookup.MimeRegistration;
40+
import org.netbeans.spi.editor.completion.CompletionProvider;
41+
import org.netbeans.spi.editor.completion.CompletionResultSet;
42+
import org.netbeans.spi.editor.completion.CompletionTask;
43+
import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
44+
import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
45+
import org.openide.util.Exceptions;
46+
47+
@MimeRegistration(mimeType = "text/x-glsl", service = CompletionProvider.class)
48+
public class GlslCompletionProvider implements CompletionProvider {
49+
50+
@Override
51+
public CompletionTask createTask(int queryType, JTextComponent component) {
52+
if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE) {
53+
return null;
54+
}
55+
56+
return new AsyncCompletionTask(new AsyncCompletionQuery() {
57+
@Override
58+
protected void query(CompletionResultSet completionResultSet,
59+
Document document, int caretOffset) {
60+
61+
String filter = getFilter(caretOffset, document);
62+
63+
// TODO: get the posibilities
64+
// TODO: filter out the special chars
65+
66+
completionResultSet.finish();
67+
}
68+
69+
private String getFilter(int caretOffset, Document document) {
70+
try {
71+
final StyledDocument bDoc = (StyledDocument) document;
72+
final int lineStartOffset = getRowFirstNonWhite(bDoc, caretOffset);
73+
final char[] line = bDoc.getText(lineStartOffset, caretOffset - lineStartOffset).toCharArray();
74+
final int whiteOffset = indexOfWhite(line);
75+
76+
return new String(line, whiteOffset + 1, line.length - whiteOffset - 1);
77+
} catch (BadLocationException ex) {
78+
Exceptions.printStackTrace(ex);
79+
}
80+
81+
return "";
82+
}
83+
}, component);
84+
}
85+
86+
private static int getRowFirstNonWhite(StyledDocument doc, int offset)
87+
throws BadLocationException {
88+
Element lineElement = doc.getParagraphElement(offset);
89+
int start = lineElement.getStartOffset();
90+
while (start + 1 < lineElement.getEndOffset()) {
91+
try {
92+
if (doc.getText(start, 1).charAt(0) != ' ') {
93+
break;
94+
}
95+
} catch (BadLocationException ex) {
96+
throw (BadLocationException) new BadLocationException(
97+
"calling getText(" + start + ", " + (start + 1)
98+
+ ") on doc of length: " + doc.getLength(), start
99+
).initCause(ex);
100+
}
101+
start++;
102+
}
103+
return start;
104+
}
105+
106+
private static int indexOfWhite(char[] line) {
107+
int i = line.length;
108+
while (--i > -1) {
109+
final char c = line[i];
110+
if (Character.isWhitespace(c)) {
111+
return i;
112+
}
113+
}
114+
return -1;
115+
}
116+
117+
@Override
118+
public int getAutoQueryTypes(JTextComponent jtc, String string) {
119+
return 0;
120+
}
121+
}

0 commit comments

Comments
 (0)