本文整理匯總了TypeScript中vscode-languageserver-types.TextDocument.offsetAt方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript TextDocument.offsetAt方法的具體用法?TypeScript TextDocument.offsetAt怎麽用?TypeScript TextDocument.offsetAt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類vscode-languageserver-types.TextDocument
的用法示例。
在下文中一共展示了TextDocument.offsetAt方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: provideCompletionItems
export function provideCompletionItems(document: TextDocument, position: Position): CompletionList {
const start = document.offsetAt(Position.create(position.line, 0));
const end = document.offsetAt(position);
const text = document.getText();
const currentWord = text.slice(start, end).trim();
const value = isValue(cssSchema, currentWord);
let completions: CompletionItem[] = [];
if (value) {
const values = getValues(cssSchema, currentWord);
const symbols = getAllSymbols(text, currentWord, position).filter(
item => item.kind === CompletionItemKind.Variable || item.kind === CompletionItemKind.Function
);
completions = completions.concat(values, symbols, builtIn);
} else {
const atRules = getAtRules(cssSchema, currentWord);
const properties = getProperties(cssSchema, currentWord, false);
const symbols = getAllSymbols(text, currentWord, position).filter(
item => item.kind !== CompletionItemKind.Variable
);
completions = completions.concat(properties, atRules, symbols);
}
return {
isIncomplete: false,
items: completions
};
}
示例2:
let sortedEdits = edits.sort((a, b) => {
let startDiff = document.offsetAt(b.range.start) - document.offsetAt(a.range.start);
if (startDiff === 0) {
return document.offsetAt(b.range.end) - document.offsetAt(a.range.end);
}
return startDiff;
});
示例3: getValueAndRange
function getValueAndRange(document: TextDocument, currRange: Range): { value: string; range: Range } {
let value = document.getText();
let range = currRange;
if (currRange) {
const startOffset = document.offsetAt(currRange.start);
const endOffset = document.offsetAt(currRange.end);
value = value.substring(startOffset, endOffset);
} else {
range = Range.create(Position.create(0, 0), document.positionAt(value.length));
}
return { value, range };
}
示例4: getApplicableRanges
function getApplicableRanges(position: Position): number[][] {
let currNode = stylesheet.findChildAtOffset(document.offsetAt(position), true);
if (!currNode) {
return [];
}
const result = [];
while (currNode) {
if (
currNode.parent &&
currNode.offset === currNode.parent.offset &&
currNode.end === currNode.parent.end
) {
currNode = currNode.parent;
continue;
}
if (currNode.type === NodeType.Declarations) {
result.push([currNode.offset + 1, currNode.end - 1]);
} else {
result.push([currNode.offset, currNode.end]);
}
currNode = currNode.parent;
}
return result;
}
示例5: getLanguageAtPosition
function getLanguageAtPosition(document: TextDocument, regions: EmbeddedRegion[], position: Position): string {
const offset = document.offsetAt(position);
for (const region of regions) {
if (region.start <= offset) {
if (offset <= region.end) {
return region.languageId;
}
} else {
break;
}
}
return 'paperclip';
}
示例6: hasHoverAt
hasHoverAt(label: string, offset: number) {
const contents = this.hover.contents;
if (Array.isArray(contents) || typeof contents === 'string') {
assert(contents.length !== 0, 'expect hover, but get nothing');
} else {
assert(contents.value.length !== 0, 'expect hover, but get nothing');
}
const strOrMarked = Array.isArray(contents) ? contents[0] : contents;
const str = typeof strOrMarked === 'string' ? strOrMarked : strOrMarked.value;
assert.equal(str, label);
const hover = this.hover;
assert.equal(this.document.offsetAt(hover.range!.start), offset);
}
示例7: getLanguageRanges
function getLanguageRanges(document: TextDocument, regions: EmbeddedRegion[], range: Range): LanguageRange[] {
const result: LanguageRange[] = [];
let currentPos = range ? range.start : Position.create(0, 0);
let currentOffset = range ? document.offsetAt(range.start) : 0;
const endOffset = range ? document.offsetAt(range.end) : document.getText().length;
for (const region of regions) {
if (region.end > currentOffset && region.start < endOffset) {
const start = Math.max(region.start, currentOffset);
const startPos = document.positionAt(start);
if (currentOffset < region.start) {
result.push({
start: currentPos,
end: startPos,
languageId: 'paperclip'
});
}
const end = Math.min(region.end, endOffset);
const endPos = document.positionAt(end);
if (end > region.start) {
result.push({
start: startPos,
end: endPos,
languageId: region.languageId
});
}
currentOffset = end;
currentPos = endPos;
}
}
if (currentOffset < endOffset) {
const endPos = range ? range.end : document.positionAt(endOffset);
result.push({
start: currentPos,
end: endPos,
languageId: 'paperclip'
});
}
return result;
}
示例8: applyEdits
export function applyEdits(document: TextDocument, edits: TextEdit[]): string {
let text = document.getText();
let sortedEdits = mergeSort(edits, (a, b) => {
let diff = a.range.start.line - b.range.start.line;
if (diff === 0) {
return a.range.start.character - b.range.start.character;
}
return 0;
});
let lastModifiedOffset = text.length;
for (let i = sortedEdits.length - 1; i >= 0; i--) {
let e = sortedEdits[i];
let startOffset = document.offsetAt(e.range.start);
let endOffset = document.offsetAt(e.range.end);
if (endOffset <= lastModifiedOffset) {
text = text.substring(0, startOffset) + e.newText + text.substring(endOffset, text.length);
} else {
throw new Error('Ovelapping edit');
}
lastModifiedOffset = startOffset;
}
return text;
}
示例9: format
export function format(languageModes: LanguageModes, document: TextDocument, formatRange: Range, formattingOptions: FormattingOptions, enabledModes: { [mode: string]: boolean }) {
// run the html formatter on the full range and pass the result content to the embedded formatters.
// from the final content create a single edit
// advantages of this approach are
// - correct indents in the html document
// - correct initial indent for embedded formatters
// - no worrying of overlapping edits
// perform a html format and apply changes to a new document
let htmlMode = languageModes.getMode('html');
let htmlEdits = htmlMode.format(document, formatRange, formattingOptions);
let htmlFormattedContent = applyEdits(document, htmlEdits);
let newDocument = TextDocument.create(document.uri + '.tmp', document.languageId, document.version, htmlFormattedContent);
try {
// run embedded formatters on html formatted content: - formatters see correct initial indent
let afterFormatRangeLength = document.getText().length - document.offsetAt(formatRange.end); // length of unchanged content after replace range
let newFormatRange = Range.create(formatRange.start, newDocument.positionAt(htmlFormattedContent.length - afterFormatRangeLength));
let embeddedRanges = languageModes.getModesInRange(newDocument, newFormatRange);
let embeddedEdits: TextEdit[] = [];
for (let r of embeddedRanges) {
let mode = r.mode;
if (mode && mode.format && enabledModes[mode.getId()] && !r.attributeValue) {
let edits = mode.format(newDocument, r, formattingOptions);
for (let edit of edits) {
embeddedEdits.push(edit);
}
}
};
if (embeddedEdits.length === 0) {
return htmlEdits;
}
// apply all embedded format edits and create a single edit for all changes
let resultContent = applyEdits(newDocument, embeddedEdits);
let resultReplaceText = resultContent.substring(document.offsetAt(formatRange.start), resultContent.length - afterFormatRangeLength);
return [TextEdit.replace(formatRange, resultReplaceText)];
} finally {
languageModes.onDocumentRemoved(newDocument);
}
}