This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 247085
Collapse All | Expand All

(-)db.sql.editor/nbproject/project.xml (+19 lines)
Lines 294-299 Link Here
294
                        <compile-dependency/>
294
                        <compile-dependency/>
295
                    </test-dependency>
295
                    </test-dependency>
296
                    <test-dependency>
296
                    <test-dependency>
297
                        <code-name-base>org.netbeans.modules.csl.api</code-name-base>
298
                        <recursive/>
299
                        <compile-dependency/>
300
                        <test/>
301
                    </test-dependency>
302
                    <test-dependency>
297
                        <code-name-base>org.netbeans.modules.db</code-name-base>
303
                        <code-name-base>org.netbeans.modules.db</code-name-base>
298
                        <compile-dependency/>
304
                        <compile-dependency/>
299
                        <test/>
305
                        <test/>
Lines 304-309 Link Here
304
                        <test/>
310
                        <test/>
305
                    </test-dependency>
311
                    </test-dependency>
306
                    <test-dependency>
312
                    <test-dependency>
313
                        <code-name-base>org.netbeans.modules.editor.mimelookup</code-name-base>
314
                        <compile-dependency/>
315
                    </test-dependency>
316
                    <test-dependency>
317
                        <code-name-base>org.netbeans.modules.editor.mimelookup.impl</code-name-base>
318
                        <compile-dependency/>
319
                    </test-dependency>
320
                    <test-dependency>
307
                        <code-name-base>org.netbeans.modules.lexer</code-name-base>
321
                        <code-name-base>org.netbeans.modules.lexer</code-name-base>
308
                        <compile-dependency/>
322
                        <compile-dependency/>
309
                        <test/>
323
                        <test/>
Lines 316-321 Link Here
316
                    <test-dependency>
330
                    <test-dependency>
317
                        <code-name-base>org.netbeans.modules.projectapi.nb</code-name-base>
331
                        <code-name-base>org.netbeans.modules.projectapi.nb</code-name-base>
318
                    </test-dependency>
332
                    </test-dependency>
333
                    <test-dependency>
334
                        <code-name-base>org.openide.util.lookup</code-name-base>
335
                        <compile-dependency/>
336
                        <test/>
337
                    </test-dependency>
319
                </test-type>
338
                </test-type>
320
            </test-dependencies>
339
            </test-dependencies>
321
            <friend-packages>
340
            <friend-packages>
(-)db.sql.editor/src/org/netbeans/modules/db/sql/editor/OptionsUtils.java (-1 / +1 lines)
Lines 54-60 Link Here
54
 * @author Tomas Mysik
54
 * @author Tomas Mysik
55
 */
55
 */
56
public final class OptionsUtils {
56
public final class OptionsUtils {
57
    private static final String PAIR_CHARACTERS_COMPLETION = "pair-characters-completion";
57
    public static final String PAIR_CHARACTERS_COMPLETION = "pair-characters-completion";
58
    private static final AtomicBoolean INITED = new AtomicBoolean(false);
58
    private static final AtomicBoolean INITED = new AtomicBoolean(false);
59
59
60
    private static final PreferenceChangeListener PREFERENCES_TRACKER = new PreferenceChangeListener() {
60
    private static final PreferenceChangeListener PREFERENCES_TRACKER = new PreferenceChangeListener() {
(-)db.sql.editor/src/org/netbeans/modules/db/sql/editor/SQLTypedTextInterceptor.java (-25 / +42 lines)
Lines 48-53 Link Here
48
import javax.swing.text.Document;
48
import javax.swing.text.Document;
49
import org.netbeans.api.editor.mimelookup.MimeRegistration;
49
import org.netbeans.api.editor.mimelookup.MimeRegistration;
50
import org.netbeans.api.editor.mimelookup.MimeRegistrations;
50
import org.netbeans.api.editor.mimelookup.MimeRegistrations;
51
import org.netbeans.api.lexer.Token;
51
import org.netbeans.api.lexer.TokenSequence;
52
import org.netbeans.api.lexer.TokenSequence;
52
import org.netbeans.editor.BaseDocument;
53
import org.netbeans.editor.BaseDocument;
53
import org.netbeans.modules.db.sql.editor.completion.SQLCompletionEnv;
54
import org.netbeans.modules.db.sql.editor.completion.SQLCompletionEnv;
Lines 70-76 Link Here
70
71
71
    @Override
72
    @Override
72
    public boolean beforeInsert(final Context context) throws BadLocationException {
73
    public boolean beforeInsert(final Context context) throws BadLocationException {
73
        if(! OptionsUtils.isPairCharactersCompletion()) {
74
        if (!OptionsUtils.isPairCharactersCompletion()) {
74
        return false;
75
        return false;
75
    }
76
    }
76
77
Lines 80-109 Link Here
80
    @Override
81
    @Override
81
            public Boolean call() {
82
            public Boolean call() {
82
                int caretOffset = context.getOffset();
83
                int caretOffset = context.getOffset();
83
                String str = context.getText(); // guaranteed to be one character
84
                char typedChar = context.getText().charAt(0); // guaranteed to be one character
84
85
85
                Character nextChar;
86
                Character nextChar;
86
                Character prevChar;
87
                try {
87
                try {
88
                    nextChar = doc.getText(caretOffset, 1).charAt(0);
88
                    nextChar = doc.getText(caretOffset, 1).charAt(0);
89
                } catch (BadLocationException ex) {
89
                } catch (BadLocationException ex) {
90
                    nextChar = null;
90
                    nextChar = null;
91
                }
91
                }
92
                try {
92
93
                    prevChar = doc.getText(caretOffset - 1, 1).charAt(0);
93
                if (nextChar != typedChar) {
94
                } catch (BadLocationException ex) {
94
                    return false;
95
                    prevChar = null;
96
                }
95
                }
97
96
98
                if (nextChar == str.charAt(0) && (
99
                        (prevChar == '(' && nextChar == ')') ||
100
                        SQLLexer.isEndStringQuoteChar(prevChar, nextChar) ||
101
                        SQLLexer.isEndIdentifierQuoteChar(prevChar, nextChar)
102
                        )) {
103
                    Caret c = context.getComponent().getCaret();
97
                    Caret c = context.getComponent().getCaret();
98
99
                if (typedChar == ')') {
104
                    c.setDot(c.getDot() + 1);
100
                    c.setDot(c.getDot() + 1);
105
                    return true;
101
                    return true;
106
                }
102
                }
103
104
                SQLCompletionEnv compEnv = SQLCompletionEnv.forDocument(doc, caretOffset);
105
                TokenSequence ts = compEnv.getTokenSequence();
106
                ts.move(compEnv.getCaretOffset());
107
                if (ts.moveNext()) {
108
                    Token currentToken = ts.token();
109
                    if (currentToken.id() == SQLTokenId.IDENTIFIER
110
                            || currentToken.id() == SQLTokenId.STRING) {
111
                        char quoteStart = currentToken.text().charAt(0);
112
                        if (SQLLexer.isEndStringQuoteChar(quoteStart, nextChar)
113
                                || SQLLexer.isEndIdentifierQuoteChar(quoteStart, nextChar)) {
114
                            c.setDot(c.getDot() + 1);
115
                            return true;
116
                        }
117
                    }
118
                }
119
107
                return false;
120
                return false;
108
            }
121
            }
109
        };
122
        };
Lines 131-137 Link Here
131
144
132
    @Override
145
    @Override
133
    public void afterInsert(final Context context) throws BadLocationException {
146
    public void afterInsert(final Context context) throws BadLocationException {
134
        if(! OptionsUtils.isPairCharactersCompletion()) {
147
        if (!OptionsUtils.isPairCharactersCompletion()) {
135
            return;
148
            return;
136
        }
149
        }
137
        
150
        
Lines 152-177 Link Here
152
165
153
                if (!str.isEmpty()) {
166
                if (!str.isEmpty()) {
154
                    char insertedChar = str.charAt(str.length() - 1);
167
                    char insertedChar = str.charAt(str.length() - 1);
155
                    if ( (SQLLexer.isStartIdentifierQuoteChar(insertedChar)
168
                    if ((SQLLexer.isStartIdentifierQuoteChar(insertedChar)
156
                            && (nextChar == null || Character.isWhitespace(nextChar) || nextChar.equals('.')))
169
                            && (nextChar == null
157
                            ||
170
                            || Character.isWhitespace(nextChar)
158
                         (SQLLexer.isStartStringQuoteChar(insertedChar)
171
                            || nextChar.equals('.')))
159
                            && (nextChar == null || Character.isWhitespace(nextChar)))
172
                            || (SQLLexer.isStartStringQuoteChar(insertedChar)
160
                            ) {  //NOI18N
173
                            && (nextChar == null
174
                            || Character.isWhitespace(nextChar)))) {  //NOI18N
161
                        if (canCompleteQuote(doc, caretOffset)) {
175
                        if (canCompleteQuote(doc, caretOffset)) {
162
                            try {
176
                            try {
163
                                // add pair quote
177
                                // add pair quote
164
                                doc.insertString(caretOffset + str.length(), String.valueOf((char) SQLLexer.getMatchingQuote(insertedChar)), null);
178
                                doc.insertString(caretOffset + str.length(), String.valueOf((char) SQLLexer.getMatchingQuote(insertedChar)), null);
165
                                context.getComponent().getCaret().setDot(caretOffset + str.length());
179
                                context.getComponent().getCaret().setDot(caretOffset
180
                                        + str.length());
166
                            } catch (BadLocationException ex) {
181
                            } catch (BadLocationException ex) {
167
                            }
182
                            }
168
                        }
183
                        }
169
                    } else if (insertedChar == '(' && (nextChar == null || Character.isWhitespace(nextChar))) {
184
                    } else if (insertedChar == '(' && (nextChar == null
185
                            || Character.isWhitespace(nextChar))) {
170
                        if (canCompleteBrace(doc, caretOffset)) {
186
                        if (canCompleteBrace(doc, caretOffset)) {
171
                            try {
187
                            try {
172
                                // add pair quote
188
                                // add pair quote
173
                                doc.insertString(caretOffset + str.length(), ")", null);
189
                                doc.insertString(caretOffset + str.length(), ")", null);
174
                                context.getComponent().getCaret().setDot(caretOffset + str.length());
190
                                context.getComponent().getCaret().setDot(caretOffset
191
                                        + str.length());
175
                            } catch (BadLocationException ex) {
192
                            } catch (BadLocationException ex) {
176
                            }
193
                            }
177
                        }
194
                        }
Lines 180-186 Link Here
180
            }
197
            }
181
        };
198
        };
182
199
183
        if(doc instanceof BaseDocument) {
200
        if (doc instanceof BaseDocument) {
184
            ((BaseDocument) doc).runAtomic(r);
201
            ((BaseDocument) doc).runAtomic(r);
185
        } else {
202
        } else {
186
            r.run();
203
            r.run();
Lines 194-200 Link Here
194
    private static boolean canCompleteQuote(Document doc, int caretOffset) {
211
    private static boolean canCompleteQuote(Document doc, int caretOffset) {
195
        SQLCompletionEnv env = SQLCompletionEnv.forDocument(doc, caretOffset);
212
        SQLCompletionEnv env = SQLCompletionEnv.forDocument(doc, caretOffset);
196
        TokenSequence<SQLTokenId> seq = env.getTokenSequence();
213
        TokenSequence<SQLTokenId> seq = env.getTokenSequence();
197
        if (seq.move(caretOffset) == 0 && seq.movePrevious()) {
214
        if (seq.move(env.getCaretOffset()) == 0 && seq.movePrevious()) {
198
            switch (seq.token().id()) {
215
            switch (seq.token().id()) {
199
                case WHITESPACE:
216
                case WHITESPACE:
200
                case DOT:
217
                case DOT:
Lines 207-213 Link Here
207
    private static boolean canCompleteBrace(Document doc, int caretOffset) {
224
    private static boolean canCompleteBrace(Document doc, int caretOffset) {
208
        SQLCompletionEnv env = SQLCompletionEnv.forDocument(doc, caretOffset);
225
        SQLCompletionEnv env = SQLCompletionEnv.forDocument(doc, caretOffset);
209
        TokenSequence<SQLTokenId> seq = env.getTokenSequence();
226
        TokenSequence<SQLTokenId> seq = env.getTokenSequence();
210
        if (seq.move(caretOffset) == 0 && seq.movePrevious()) {
227
        if (seq.move(env.getCaretOffset()) == 0 && seq.movePrevious()) {
211
            switch (seq.token().id()) {
228
            switch (seq.token().id()) {
212
                case WHITESPACE:
229
                case WHITESPACE:
213
                    return true;
230
                    return true;
(-)db.sql.editor/test/unit/src/META-INF/services/org.netbeans.spi.lexer.LanguageProvider (+1 lines)
Line 0 Link Here
1
org.netbeans.lib.lexer.test.TestLanguageProvider
(-)db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/editor/SQLTypedTextInterceptorTest.java (+251 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2014 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2014 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.sql.editor;
43
44
import java.util.prefs.Preferences;
45
import org.netbeans.api.editor.mimelookup.MimeLookup;
46
import org.netbeans.modules.csl.api.Formatter;
47
import org.netbeans.modules.csl.api.test.CslTestBase;
48
import org.netbeans.modules.csl.spi.DefaultLanguageConfig;
49
import org.netbeans.modules.db.sql.lexer.SQLLanguageConfig;
50
import org.netbeans.modules.db.sql.lexer.SQLTokenId;
51
52
public class SQLTypedTextInterceptorTest extends CslTestBase {
53
54
    private final Preferences prefs;
55
56
    public SQLTypedTextInterceptorTest() {
57
        super("SQLTypedTextInterceptorTest");
58
        prefs = MimeLookup.getLookup(SQLLanguageConfig.mimeType).lookup(Preferences.class);
59
    }
60
61
    @Override
62
    protected Formatter getFormatter(IndentPrefs preferences) {
63
        return null;
64
    }
65
66
    @Override
67
    protected boolean runInEQ() {
68
        // Must run in AWT thread (BaseKit.install() checks for that)
69
        return true;
70
    }
71
72
    @Override
73
    protected DefaultLanguageConfig getPreferredLanguage() {
74
        return new SQLLanguageConfig();
75
    }
76
77
    @Override
78
    protected String getPreferredMimeType() {
79
        return SQLTokenId.language().mimeType();
80
    }
81
82
    public void testBasicDisabled() throws Exception {
83
        prefs.putBoolean(OptionsUtils.PAIR_CHARACTERS_COMPLETION, false);
84
        insertChar(
85
                "select * from a where b = ^",
86
                '\'',
87
                "select * from a where b = '^",
88
                null, false);
89
        insertChar(
90
                "select * from a where b = ^",
91
                '"',
92
                "select * from a where b = \"^",
93
                null, false);
94
        insertChar(
95
                "select * from a where b = ^",
96
                '(',
97
                "select * from a where b = (^",
98
                null, false);
99
        insertChar(
100
                "select * from a where b = ^",
101
                '[',
102
                "select * from a where b = [^",
103
                null, false);
104
        insertChar(
105
                "select * from a where b = 'DEMO^'",
106
                '\'',
107
                "select * from a where b = 'DEMO'^'",
108
                null, false);
109
        insertChar(
110
                "select * from a where b = \"DEMO^\"",
111
                '"',
112
                "select * from a where b = \"DEMO\"^\"",
113
                null, false);
114
        insertChar(
115
                "select * from a where b = (DEMO^)",
116
                ')',
117
                "select * from a where b = (DEMO)^)",
118
                null, false);
119
        insertChar(
120
                "select * from a where b = [DEMO^]",
121
                ']',
122
                "select * from a where b = [DEMO]^]",
123
                null, false);
124
    }
125
126
    public void testBasicEnabled() throws Exception {
127
        prefs.putBoolean(OptionsUtils.PAIR_CHARACTERS_COMPLETION, true);
128
        insertChar(
129
                "select * from a where b = ^",
130
                '\'',
131
                "select * from a where b = '^'",
132
                null, false);
133
        insertChar(
134
                "select * from a where b = ^",
135
                '"',
136
                "select * from a where b = \"^\"",
137
                null, false);
138
        insertChar(
139
                "select * from a where b = ^",
140
                '(',
141
                "select * from a where b = (^)",
142
                null, false);
143
        insertChar(
144
                "select * from a where b = ^",
145
                '[',
146
                "select * from a where b = [^]",
147
                null, false);
148
        insertChar(
149
                "select * from a where b = 'DEMO^'",
150
                '\'',
151
                "select * from a where b = 'DEMO'^",
152
                null, false);
153
        insertChar(
154
                "select * from a where b = \"DEMO^\"",
155
                '"',
156
                "select * from a where b = \"DEMO\"^",
157
                null, false);
158
        insertChar(
159
                "select * from a where b = (DEMO^)",
160
                ')',
161
                "select * from a where b = (DEMO)^",
162
                null, false);
163
        insertChar(
164
                "select * from a where b = [DEMO^]",
165
                ']',
166
                "select * from a where b = [DEMO]^",
167
                null, false);
168
    }
169
170
    public void testEmptyCompletion() throws Exception {
171
        prefs.putBoolean(OptionsUtils.PAIR_CHARACTERS_COMPLETION, true);
172
        insertChar(
173
                "select * from a where b = '^'",
174
                '\'',
175
                "select * from a where b = ''^",
176
                null, false);
177
        insertChar(
178
                "select * from a where b = \"^\"",
179
                '"',
180
                "select * from a where b = \"\"^",
181
                null, false);
182
        insertChar(
183
                "select * from a where b = (^)",
184
                ')',
185
                "select * from a where b = ()^",
186
                null, false);
187
        insertChar(
188
                "select * from a where b = [^]",
189
                ']',
190
                "select * from a where b = []^",
191
                null, false);
192
    }
193
194
    public void testMultiStatementHandling() throws Exception {
195
        prefs.putBoolean(OptionsUtils.PAIR_CHARACTERS_COMPLETION, true);
196
        insertChar(
197
                "create table dummy(id integer, titel varchar(255)); select * from a where b = ^",
198
                '\'',
199
                "create table dummy(id integer, titel varchar(255)); select * from a where b = '^'",
200
                null, false);
201
        insertChar(
202
                "create table dummy(id integer, titel varchar(255)); select * from a where b = ^",
203
                '"',
204
                "create table dummy(id integer, titel varchar(255)); select * from a where b = \"^\"",
205
                null, false);
206
        insertChar(
207
                "create table dummy(id integer, titel varchar(255)); select * from a where b = ^",
208
                '(',
209
                "create table dummy(id integer, titel varchar(255)); select * from a where b = (^)",
210
                null, false);
211
        insertChar(
212
                "create table dummy(id integer, titel varchar(255)); select * from a where b = ^",
213
                '[',
214
                "create table dummy(id integer, titel varchar(255)); select * from a where b = [^]",
215
                null, false);
216
        insertChar(
217
                "create table dummy(id integer, titel varchar(255)); select * from a where b = 'DEMO^'",
218
                '\'',
219
                "create table dummy(id integer, titel varchar(255)); select * from a where b = 'DEMO'^",
220
                null, false);
221
        insertChar(
222
                "create table dummy(id integer, titel varchar(255)); select * from a where b = \"DEMO^\"",
223
                '"',
224
                "create table dummy(id integer, titel varchar(255)); select * from a where b = \"DEMO\"^",
225
                null, false);
226
        insertChar(
227
                "create table dummy(id integer, titel varchar(255)); select * from a where b = (DEMO^)",
228
                ')',
229
                "create table dummy(id integer, titel varchar(255)); select * from a where b = (DEMO)^",
230
                null, false);
231
        insertChar(
232
                "create table dummy(id integer, titel varchar(255)); select * from a where b = [DEMO^]",
233
                ']',
234
                "create table dummy(id integer, titel varchar(255)); select * from a where b = [DEMO]^",
235
                null, false);
236
    }
237
238
    public void testAlwaysSwallowClosingBraces() throws Exception {
239
        prefs.putBoolean(OptionsUtils.PAIR_CHARACTERS_COMPLETION, true);
240
        insertChar(
241
                "select * from a where (b = a^)",
242
                ')',
243
                "select * from a where (b = a)^",
244
                null, false);
245
        insertChar(
246
                "select * from a where (b = a)^)",
247
                ')',
248
                "select * from a where (b = a))^",
249
                null, false);
250
    }
251
}

Return to bug 247085