Skip to content

Commit fc4a2dd

Browse files
snjezargrunber
authored andcommitted
Autoclose multiline strings (JEP 368)
Signed-off-by: Snjezana Peco <[email protected]>
1 parent d8a989b commit fc4a2dd

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

language-configuration.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
[
1919
"(",
2020
")"
21+
],
22+
[
23+
"\"\"\"",
24+
"\"\"\""
2125
]
2226
],
2327
"autoClosingPairs": [
@@ -47,6 +51,13 @@
4751
"notIn": [
4852
"string"
4953
]
54+
},
55+
{
56+
"open": "\"\"\"",
57+
"close": "\"\"\";",
58+
"notIn": [
59+
"comment"
60+
]
5061
}
5162
],
5263
"surroundingPairs": [

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { initialize as initializeRecommendation } from './recommendation';
1414
import { Commands } from './commands';
1515
import { ExtensionAPI, ClientStatus } from './extension.api';
1616
import { getJavaConfiguration, deleteDirectory, getBuildFilePatterns, getInclusionPatternsFromNegatedExclusion, convertToGlob, getExclusionBlob, ensureExists } from './utils';
17-
import { onConfigurationChange, getJavaServerMode, ServerMode, ACTIVE_BUILD_TOOL_STATE } from './settings';
17+
import { onConfigurationChange, getJavaServerMode, ServerMode, ACTIVE_BUILD_TOOL_STATE, handleTextBlockClosing } from './settings';
1818
import { logger, initializeLogFile } from './log';
1919
import glob = require('glob');
2020
import { SyntaxLanguageClient } from './syntaxLanguageClient';
@@ -464,6 +464,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
464464
}
465465
}));
466466
}
467+
context.subscriptions.push(workspace.onDidChangeTextDocument(event => handleTextBlockClosing(event.document, event.contentChanges)));
467468
});
468469
});
469470
}

src/settings.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as fs from 'fs';
44
import * as path from 'path';
5-
import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget, env, ExtensionContext, TextEditor, Range, Disposable, WorkspaceFolder } from 'vscode';
5+
import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget, env, ExtensionContext, TextEditor, Range, Disposable, WorkspaceFolder, TextDocument, Position, SnippetString, TextLine } from 'vscode';
66
import { Commands } from './commands';
77
import { cleanWorkspaceFileName } from './extension';
88
import { ensureExists, getJavaConfiguration } from './utils';
@@ -287,3 +287,35 @@ function unregisterGradleWrapperPromptDialog(sha256: string) {
287287
gradleWrapperPromptDialogs.splice(index, 1);
288288
}
289289
}
290+
291+
export function handleTextBlockClosing(document: TextDocument, changes: readonly import("vscode").TextDocumentContentChangeEvent[]): any {
292+
const activeTextEditor = window.activeTextEditor;
293+
const activeDocument = activeTextEditor && activeTextEditor.document;
294+
if (document !== activeDocument || changes.length === 0 || document.languageId !== 'java') {
295+
return;
296+
}
297+
const lastChange = changes[changes.length - 1];
298+
if (lastChange.text === null || lastChange.text.length <= 0) {
299+
return;
300+
}
301+
if (lastChange.text !== '"""";') {
302+
return;
303+
}
304+
const selection = activeTextEditor.selection.active;
305+
if (selection !== null) {
306+
const start = new Position(selection.line, selection.character - 2);
307+
const end = new Position(selection.line, selection.character + 4);
308+
const range = new Range(start, end);
309+
const activeText = activeDocument.getText(range);
310+
if (activeText === '""""""') {
311+
const tabSize = <number>activeTextEditor.options.tabSize!;
312+
const tabSpaces = <boolean>activeTextEditor.options.insertSpaces!;
313+
const indentLevel = 2;
314+
const indentSize = tabSpaces ? indentLevel * tabSize : indentLevel;
315+
const repeatChar = tabSpaces ? ' ' : '\t';
316+
const text = `\n${repeatChar.repeat(indentSize)}\$\{0\}\n${repeatChar.repeat(indentSize)}`;
317+
const position = new Position(selection.line, selection.character + 1);
318+
activeTextEditor.insertSnippet(new SnippetString(text), position);
319+
}
320+
}
321+
}

0 commit comments

Comments
 (0)