1
0
mirror of https://github.com/salexdv/bsl_console.git synced 2024-11-24 08:33:29 +02:00

Форматирование выделенного фрагмента #245

This commit is contained in:
salexdv 2021-11-29 13:55:50 +03:00
parent a6cae40db5
commit 93c848b5c5
2 changed files with 82 additions and 29 deletions

View File

@ -5624,6 +5624,34 @@ class bslHelper {
} }
/**
* Returns array of words from string
*
* @param {string} str string
*
* @returns {array} words
*/
static getWordsFromFormatString(str) {
const comment = str.indexOf('//');
if (0 <= comment)
str = str.substr(0, comment);
str = str.replace(/"([\s\S]+)?"/u, '');
str = str.replace(/"([\s\S]+)?$/u, '');
str = str.replace(/\|([\s\S]+)?"/u, '');
str = str.replace(/\|([\s\S]+)?$/u, '');
const semi = str.indexOf(';');
if (0 <= semi)
str = str.substr(0, semi);
return str.trim().split(' ');
}
/** /**
* Code formatter * Code formatter
* *
@ -5637,7 +5665,7 @@ class bslHelper {
const startWords = [ const startWords = [
'если', 'для', 'пока', 'функция', 'процедура', 'попытка', 'если', 'для', 'пока', 'функция', 'процедура', 'попытка',
'if', 'for', 'while', 'function', 'procedure', 'try' 'if', 'for', 'while', 'function', 'procedure', 'try'
]; ];
const stopWords = [ const stopWords = [
@ -5650,39 +5678,64 @@ class bslHelper {
'except', 'else', 'elseif' 'except', 'else', 'elseif'
]; ];
const strings = model.getValue().split('\n'); let format_range = model.getFullModelRange();
const selection = editor.getSelection();
const selected_text = model.getValueInRange(selection).trim();
let offset = 0; let offset = 0;
let strings = '';
if (selected_text) {
format_range = new monaco.Range(
selection.startLineNumber,
1,
selection.endLineNumber,
model.getLineMaxColumn(selection.endLineNumber)
);
strings = model.getValueInRange(format_range).split('\n');
let line_number = selection.startLineNumber - 1;
while (0 < line_number && offset == 0) {
let str = model.getLineContent(line_number)
let words = bslHelper.getWordsFromFormatString(str);
let word_i = 0;
while (word_i < words.length && offset == 0) {
let word = words[word_i].toLowerCase();
if (startWords.includes(word)) {
str = model.normalizeIndentation(str);
offset = str.match(/^(\t*)/)[0].split('\t').length;
}
word_i++;
}
line_number--;
}
}
else {
strings = model.getValue().split('\n');
}
strings.forEach(function (str, index) { strings.forEach(function (str, index) {
let original = str; let original = str;
let comment = str.indexOf('//'); const words = bslHelper.getWordsFromFormatString(str);
if (0 <= comment)
str = str.substr(0, comment);
str = str.replace(/"([\s\S]+)?"/u, '');
str = str.replace(/"([\s\S]+)?$/u, '');
str = str.replace(/\|([\s\S]+)?"/u, '');
str = str.replace(/\|([\s\S]+)?$/u, '');
let semi = str.indexOf(';');
if (0 <= semi)
str = str.substr(0, semi);
let words = str.trim().split(' ');
let word_i = 0; let word_i = 0;
let delta = offset; let delta = offset;
while (word_i < words.length) { while (word_i < words.length) {
let word = words[word_i].toLowerCase(); let word = words[word_i].toLowerCase();
if (startWords.includes(word)) if (startWords.includes(word))
offset++; offset++;
if (stopWords.includes(word)) if (stopWords.includes(word))
offset = Math.max(0, offset - 1); offset = Math.max(0, offset - 1);
@ -5693,9 +5746,9 @@ class bslHelper {
} }
delta = offset - delta; delta = offset - delta;
let strOffset = 0 < delta ? offset - 1 : offset; let strOffset = 0 < delta ? offset - 1 : offset;
result = result + '\t'.repeat(strOffset) + original.trim(); result = result + '\t'.repeat(strOffset) + original.trim();
if (index < strings.length - 1) if (index < strings.length - 1)
result += '\n'; result += '\n';
@ -5704,7 +5757,10 @@ class bslHelper {
}); });
return result; return [{
text: result,
range: format_range
}];
} }
/** /**

View File

@ -459,10 +459,7 @@ define([], function () {
}, },
formatProvider: { formatProvider: {
provideDocumentFormattingEdits: function (model, options, token) { provideDocumentFormattingEdits: function (model, options, token) {
return [{ return bslHelper.formatCode(model);
text: bslHelper.formatCode(model),
range: model.getFullModelRange()
}];
} }
}, },
codeLenses: { codeLenses: {