@volar/language-service
Advanced tools
Comparing version 1.2.0-alpha.10 to 1.2.0-alpha.11
@@ -23,9 +23,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
range !== null && range !== void 0 ? range : (range = vscode.Range.create(document.positionAt(0), document.positionAt(document.getText().length))); | ||
const initialIndentLanguageId = (_b = yield ((_a = context.env.configurationHost) === null || _a === void 0 ? void 0 : _a.getConfiguration('volar.format.initialIndent'))) !== null && _b !== void 0 ? _b : { html: true }; | ||
const source = context.documents.getSourceByUri(document.uri); | ||
if (!source) { | ||
return onTypeParams | ||
? yield tryFormat(false, document, onTypeParams.position, onTypeParams.ch) | ||
: yield tryFormat(false, document, range, undefined); | ||
? yield tryFormat(document, onTypeParams.position, onTypeParams.ch) | ||
: yield tryFormat(document, range, undefined); | ||
} | ||
const initialIndentLanguageId = (_b = yield ((_a = context.env.configurationHost) === null || _a === void 0 ? void 0 : _a.getConfiguration('volar.format.initialIndent'))) !== null && _b !== void 0 ? _b : { html: true }; | ||
const originalSnapshot = source.snapshot; | ||
@@ -41,6 +41,9 @@ const rootVirtualFile = source.root; | ||
let edits = []; | ||
let toPatchIndentUri; | ||
const toPatchIndentUris = []; | ||
for (const embedded of embeddedFiles) { | ||
if (!embedded.capabilities.documentFormatting) | ||
continue; | ||
const enabledOnType = typeof embedded.capabilities.documentFormatting === 'object' ? !!embedded.capabilities.documentFormatting.onType : true; | ||
if (onTypeParams && !enabledOnType) | ||
continue; | ||
const maps = [...context.documents.getMapsByVirtualFileName(embedded.fileName)]; | ||
@@ -50,30 +53,19 @@ const map = (_c = maps.find(map => map[1].sourceFileDocument.uri === document.uri)) === null || _c === void 0 ? void 0 : _c[1]; | ||
continue; | ||
let _edits; | ||
let virtualCodeEdits; | ||
if (onTypeParams) { | ||
const embeddedPosition = map.toGeneratedPosition(onTypeParams.position); | ||
if (embeddedPosition) { | ||
_edits = yield tryFormat(true, map.virtualFileDocument, embeddedPosition, onTypeParams.ch); | ||
virtualCodeEdits = yield tryFormat(map.virtualFileDocument, embeddedPosition, onTypeParams.ch); | ||
} | ||
} | ||
else { | ||
let genRange = map.toGeneratedRange(range); | ||
if (!genRange) { | ||
const firstMapping = map.map.mappings.sort((a, b) => a.sourceRange[0] - b.sourceRange[0])[0]; | ||
const lastMapping = map.map.mappings.sort((a, b) => b.sourceRange[0] - a.sourceRange[0])[0]; | ||
if (firstMapping && document.offsetAt(range.start) < firstMapping.sourceRange[0] | ||
&& lastMapping && document.offsetAt(range.end) > lastMapping.sourceRange[1]) { | ||
genRange = { | ||
start: map.virtualFileDocument.positionAt(firstMapping.generatedRange[0]), | ||
end: map.virtualFileDocument.positionAt(lastMapping.generatedRange[1]), | ||
}; | ||
} | ||
} | ||
if (genRange) { | ||
toPatchIndentUri = map.virtualFileDocument.uri; | ||
_edits = yield tryFormat(true, map.virtualFileDocument, genRange); | ||
} | ||
virtualCodeEdits = yield tryFormat(map.virtualFileDocument, { | ||
start: map.virtualFileDocument.positionAt(0), | ||
end: map.virtualFileDocument.positionAt(map.virtualFileDocument.getText().length), | ||
}); | ||
} | ||
if (!_edits) | ||
if (!virtualCodeEdits) | ||
continue; | ||
for (const textEdit of _edits) { | ||
toPatchIndentUris.push(map.virtualFileDocument.uri); | ||
for (const textEdit of virtualCodeEdits) { | ||
const range = map.toSourceRange(textEdit.range); | ||
@@ -88,2 +80,3 @@ if (range) { | ||
} | ||
edits = edits.filter(edit => (0, common_1.isInsideRange)(range, edit.range)); | ||
if (edits.length > 0) { | ||
@@ -95,10 +88,14 @@ const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits); | ||
} | ||
if (toPatchIndentUri) { | ||
for (const [_, map] of context.documents.getMapsByVirtualFileUri(toPatchIndentUri)) { | ||
const indentEdits = patchInterpolationIndent(document, map.map); | ||
if (indentEdits.length > 0) { | ||
const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits); | ||
document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText); | ||
context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined); | ||
edited = true; | ||
if (level > 1) { | ||
const baseIndent = options.insertSpaces ? ' '.repeat(options.tabSize) : '\t'; | ||
for (const toPatchIndentUri of toPatchIndentUris) { | ||
for (const [file, map] of context.documents.getMapsByVirtualFileUri(toPatchIndentUri)) { | ||
let indentEdits = patchInterpolationIndent(document, map.map, initialIndentLanguageId[map.virtualFileDocument.languageId] ? baseIndent : '', file.capabilities.documentFormatting); | ||
indentEdits = indentEdits.filter(edit => (0, common_1.isInsideRange)(range, edit.range)); | ||
if (indentEdits.length > 0) { | ||
const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits); | ||
document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText); | ||
context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined); | ||
edited = true; | ||
} | ||
} | ||
@@ -129,3 +126,3 @@ } | ||
} | ||
function tryFormat(isEmbedded, document, range, ch) { | ||
function tryFormat(document, range, ch) { | ||
var _a, _b; | ||
@@ -141,7 +138,7 @@ return __awaiter(this, void 0, void 0, function* () { | ||
else if (ch === undefined && vscode.Range.is(formatRange)) { | ||
edits = yield ((_b = plugin.format) === null || _b === void 0 ? void 0 : _b.call(plugin, document, formatRange, Object.assign(Object.assign({}, options), { initialIndent: isEmbedded ? !!initialIndentLanguageId[document.languageId] : false }))); | ||
edits = yield ((_b = plugin.format) === null || _b === void 0 ? void 0 : _b.call(plugin, document, formatRange, options)); | ||
} | ||
} | ||
catch (err) { | ||
console.error(err); | ||
console.warn(err); | ||
} | ||
@@ -157,10 +154,10 @@ if (!edits) | ||
exports.register = register; | ||
function patchInterpolationIndent(document, map) { | ||
function patchInterpolationIndent(document, map, initialIndent, data) { | ||
const insertFirstNewline = typeof data === 'object' ? data.insertFirstNewline : false; | ||
const insertFinalNewline = typeof data === 'object' ? data.insertFinalNewline : false; | ||
const indentTextEdits = []; | ||
for (const mapped of map.mappings) { | ||
const baseIndent = getBaseIndent(mapped.sourceRange[0]); | ||
if (baseIndent === '') { | ||
continue; | ||
} | ||
const text = document.getText().substring(mapped.sourceRange[0], mapped.sourceRange[1]); | ||
for (let i = 0; i < map.mappings.length; i++) { | ||
const mapping = map.mappings[i]; | ||
const firstLineIndent = getBaseIndent(mapping.sourceRange[0]); | ||
const text = document.getText().substring(mapping.sourceRange[0], mapping.sourceRange[1]); | ||
if (text.indexOf('\n') === -1) { | ||
@@ -170,12 +167,45 @@ continue; | ||
const lines = text.split('\n'); | ||
for (let i = 1; i < lines.length; i++) { | ||
lines[i] = baseIndent + lines[i]; | ||
const baseIndent = firstLineIndent + initialIndent; | ||
let lineOffset = lines[0].length + 1; | ||
if (insertFirstNewline && i === 0 && !text.startsWith('\n')) { | ||
indentTextEdits.push({ | ||
newText: '\n' + baseIndent, | ||
range: { | ||
start: document.positionAt(mapping.sourceRange[0]), | ||
end: document.positionAt(mapping.sourceRange[0]), | ||
}, | ||
}); | ||
} | ||
indentTextEdits.push({ | ||
newText: lines.join('\n'), | ||
range: { | ||
start: document.positionAt(mapped.sourceRange[0]), | ||
end: document.positionAt(mapped.sourceRange[1]), | ||
}, | ||
}); | ||
if (baseIndent) { | ||
for (let i = 1; i < lines.length - 1; i++) { | ||
if (lines[i] !== '') { | ||
indentTextEdits.push({ | ||
newText: baseIndent, | ||
range: { | ||
start: document.positionAt(mapping.sourceRange[0] + lineOffset), | ||
end: document.positionAt(mapping.sourceRange[0] + lineOffset), | ||
}, | ||
}); | ||
} | ||
lineOffset += lines[i].length + 1; | ||
} | ||
} | ||
if (firstLineIndent) { | ||
indentTextEdits.push({ | ||
newText: firstLineIndent, | ||
range: { | ||
start: document.positionAt(mapping.sourceRange[0] + lineOffset), | ||
end: document.positionAt(mapping.sourceRange[0] + lineOffset), | ||
}, | ||
}); | ||
} | ||
if (insertFinalNewline && i === map.mappings.length - 1 && !text.endsWith('\n')) { | ||
indentTextEdits.push({ | ||
newText: '\n', | ||
range: { | ||
start: document.positionAt(mapping.sourceRange[1]), | ||
end: document.positionAt(mapping.sourceRange[1]), | ||
}, | ||
}); | ||
} | ||
} | ||
@@ -182,0 +212,0 @@ return indentTextEdits; |
@@ -63,3 +63,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
if (data && data.version !== sourceDocument.version) { | ||
console.warn('[volar/plugin-api] diagnostic version mismatch', data.version, sourceDocument.version); | ||
// console.warn('[volar/plugin-api] diagnostic version mismatch', data.version, sourceDocument.version); | ||
return false; | ||
@@ -104,3 +104,3 @@ } | ||
if (data && data.version !== sourceDocument.version) { | ||
console.warn('[volar/rules-api] diagnostic version mismatch', data.version, sourceDocument.version); | ||
// console.warn('[volar/rules-api] diagnostic version mismatch', data.version, sourceDocument.version); | ||
continue; | ||
@@ -107,0 +107,0 @@ } |
@@ -89,5 +89,3 @@ import { LanguageContext, LanguageModule, LanguageServiceHost } from '@volar/language-core'; | ||
getSignatureHelp?(document: TextDocument, position: vscode.Position, context?: vscode.SignatureHelpContext): NullableResult<vscode.SignatureHelp>; | ||
format?(document: TextDocument, range: vscode.Range, options: vscode.FormattingOptions & { | ||
initialIndent: boolean; | ||
}): NullableResult<vscode.TextEdit[]>; | ||
format?(document: TextDocument, range: vscode.Range, options: vscode.FormattingOptions): NullableResult<vscode.TextEdit[]>; | ||
formatOnType?(document: TextDocument, position: vscode.Position, key: string, options: vscode.FormattingOptions): NullableResult<vscode.TextEdit[]>; | ||
@@ -94,0 +92,0 @@ definition?: { |
{ | ||
"name": "@volar/language-service", | ||
"version": "1.2.0-alpha.10", | ||
"version": "1.2.0-alpha.11", | ||
"main": "out/index.js", | ||
@@ -16,6 +16,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"@volar/language-core": "1.2.0-alpha.10", | ||
"@volar/shared": "1.2.0-alpha.10", | ||
"@volar/source-map": "1.2.0-alpha.10", | ||
"@volar/typescript-faster": "1.2.0-alpha.10", | ||
"@volar/language-core": "1.2.0-alpha.11", | ||
"@volar/shared": "1.2.0-alpha.11", | ||
"@volar/source-map": "1.2.0-alpha.11", | ||
"@volar/typescript-faster": "1.2.0-alpha.11", | ||
"vscode-html-languageservice": "^5.0.4", | ||
@@ -27,3 +27,3 @@ "vscode-json-languageservice": "^5.2.0", | ||
}, | ||
"gitHead": "c0b7580b14966d28d7c6df5da9baa41111f9f8d9" | ||
"gitHead": "b9b3d607df5772c00edda17cfda05281c3e7c06f" | ||
} |
211640
3885
+ Added@volar/language-core@1.2.0-alpha.11(transitive)
+ Added@volar/shared@1.2.0-alpha.11(transitive)
+ Added@volar/source-map@1.2.0-alpha.11(transitive)
+ Added@volar/typescript-faster@1.2.0-alpha.11(transitive)
- Removed@volar/language-core@1.2.0-alpha.10(transitive)
- Removed@volar/shared@1.2.0-alpha.10(transitive)
- Removed@volar/source-map@1.2.0-alpha.10(transitive)
- Removed@volar/typescript-faster@1.2.0-alpha.10(transitive)
Updated@volar/shared@1.2.0-alpha.11