本文整理汇总了TypeScript中vscode-languageserver.TextDocument.positionAt方法的典型用法代码示例。如果您正苦于以下问题:TypeScript TextDocument.positionAt方法的具体用法?TypeScript TextDocument.positionAt怎么用?TypeScript TextDocument.positionAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vscode-languageserver.TextDocument
的用法示例。
在下文中一共展示了TextDocument.positionAt方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: processResults
protected processResults(output: string): TextEdit[] {
const diffs: Diff[] = this.differ.diff_main(this.originalText, output);
const edits: TextEdit[] = [];
// VSCode wants TextEdits on the original document
// this means position only gets moved for DIFF_EQUAL and DIFF_DELETE
// as insert is new and doesn't have a position in the original
let position = 0;
for (let diff of diffs) {
const [num, str] = diff;
const startPos = this.document.positionAt(position);
switch (num) {
case DIFF_DELETE:
edits.push({
range: {
start: startPos,
end: this.document.positionAt(position + str.length),
},
newText: '',
});
position += str.length;
break;
case DIFF_INSERT:
edits.push({
range: { start: startPos, end: startPos },
newText: str,
});
break;
case DIFF_EQUAL:
position += str.length;
break;
}
// If we have a range we are doing a selection format. Thus,
// only apply patches that start within the selected range
if (this.range && num !== DIFF_EQUAL && !this.checkPositionInRange(startPos)) {
edits.pop();
}
}
return edits;
}
示例2:
.map(offsetWord => ({...offsetWord, position: textDocument.positionAt(offsetWord.offset) }))
示例3: format
export function format(document: TextDocument, range: Range, options: FormattingOptions): TextEdit[] {
const documentText = document.getText();
let initialIndentLevel: number;
let value: string;
let rangeOffset: number;
if (range) {
let startPosition = Position.create(range.start.line, 0);
rangeOffset = document.offsetAt(startPosition);
let endOffset = document.offsetAt(Position.create(range.end.line + 1, 0));
let endLineStart = document.offsetAt(Position.create(range.end.line, 0));
while (endOffset > endLineStart && isEOL(documentText, endOffset - 1)) {
endOffset--;
}
range = Range.create(startPosition, document.positionAt(endOffset));
value = documentText.substring(rangeOffset, endOffset);
initialIndentLevel = computeIndentLevel(value, 0, options);
} else {
value = documentText;
range = Range.create(Position.create(0, 0), document.positionAt(value.length));
initialIndentLevel = 0;
rangeOffset = 0;
}
let eol = getEOL(document);
let lineBreak = false;
let indentLevel = 0;
let indentValue: string;
if (options.insertSpaces) {
indentValue = repeat(' ', options.tabSize);
} else {
indentValue = '\t';
}
let scanner = Json.createScanner(value, false);
function newLineAndIndent(): string {
return eol + repeat(indentValue, initialIndentLevel + indentLevel);
}
function scanNext(): Json.SyntaxKind {
let token = scanner.scan();
lineBreak = false;
while (token === Json.SyntaxKind.Trivia || token === Json.SyntaxKind.LineBreakTrivia) {
lineBreak = lineBreak || (token === Json.SyntaxKind.LineBreakTrivia);
token = scanner.scan();
}
return token;
}
let editOperations: TextEdit[] = [];
function addEdit(text: string, startOffset: number, endOffset: number) {
if (documentText.substring(startOffset, endOffset) !== text) {
let replaceRange = Range.create(document.positionAt(startOffset), document.positionAt(endOffset));
editOperations.push(TextEdit.replace(replaceRange, text));
}
}
let firstToken = scanNext();
if (firstToken !== Json.SyntaxKind.EOF) {
let firstTokenStart = scanner.getTokenOffset() + rangeOffset;
let initialIndent = repeat(indentValue, initialIndentLevel);
addEdit(initialIndent, rangeOffset, firstTokenStart);
}
while (firstToken !== Json.SyntaxKind.EOF) {
let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
let secondToken = scanNext();
let replaceContent = '';
while (!lineBreak && (secondToken === Json.SyntaxKind.LineCommentTrivia || secondToken === Json.SyntaxKind.BlockCommentTrivia)) {
// comments on the same line: keep them on the same line, but ignore them otherwise
let commentTokenStart = scanner.getTokenOffset() + rangeOffset;
addEdit(' ', firstTokenEnd, commentTokenStart);
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
replaceContent = secondToken === Json.SyntaxKind.LineCommentTrivia ? newLineAndIndent() : '';
secondToken = scanNext();
}
if (secondToken === Json.SyntaxKind.CloseBraceToken) {
if (firstToken !== Json.SyntaxKind.OpenBraceToken) {
indentLevel--;
replaceContent = newLineAndIndent();
}
} else if (secondToken === Json.SyntaxKind.CloseBracketToken) {
if (firstToken !== Json.SyntaxKind.OpenBracketToken) {
indentLevel--;
replaceContent = newLineAndIndent();
}
} else {
switch (firstToken) {
case Json.SyntaxKind.OpenBracketToken:
case Json.SyntaxKind.OpenBraceToken:
indentLevel++;
replaceContent = newLineAndIndent();
break;
case Json.SyntaxKind.CommaToken:
case Json.SyntaxKind.LineCommentTrivia:
replaceContent = newLineAndIndent();
break;
case Json.SyntaxKind.BlockCommentTrivia:
if (lineBreak) {
//.........这里部分代码省略.........
示例4: addEdit
function addEdit(text: string, startOffset: number, endOffset: number) {
if (documentText.substring(startOffset, endOffset) !== text) {
let replaceRange = Range.create(document.positionAt(startOffset), document.positionAt(endOffset));
editOperations.push(TextEdit.replace(replaceRange, text));
}
}
示例5: generateFromLocal
function generateFromLocal(file: string, existingPath: string, startPos: number, document: TextDocument) {
if (file === lastFile2 && existingPath === lastPath2) {
return;
}
lastFile2 = file;
lastPath2 = existingPath;
localModules = [];
let currentPath = path.join(file, '../').replace(/\\/g, '/');
let workingPath = ensureEndingSlash(path.dirname(file));
let replaceRange = Range.create(document.positionAt(startPos), document.positionAt(startPos + existingPath.length));
recursiveReadDir(workspaceRoot);
function getRelatedPath(f: string): string {
let filePath = ensureEndingSlash(path.dirname(f));
filePath = filePath.charAt(0).toUpperCase() + filePath.slice(1);
let filename = path.basename(f);
let firstDiff = findFirstDiff(filePath, workingPath);
if (firstDiff < 0) {
return './' + filename;
}
let workingSubPath = workingPath.substring(firstDiff);
let fileSubPath = filePath.substring(firstDiff);
let prepend = '';
if (workingSubPath.length === 0) {
prepend += './';
}
while (workingSubPath.length > 0) {
let slashIdx = workingSubPath.lastIndexOf('/');
if (slashIdx < 0) {
break;
}
prepend += '../';
workingSubPath = workingSubPath.substring(0, slashIdx);
}
prepend += fileSubPath;
return prepend + filename;
}
function recursiveReadDir(pth: string) {
let list = listJsFilesAndDirectories(pth);
_.forEach(list.files, (f) => {
let name = getRelatedPath(f);
handleModule(name, existingPath, localModules);
});
_.forEach(list.dirs, (dir) => {
recursiveReadDir(dir);
});
}
}
示例6: getHTMLFoldingRegions
export function getHTMLFoldingRegions(htmlLanguageService: HTMLLanguageService, document: TextDocument, range: Range): FoldingRange[] {
const scanner = htmlLanguageService.createScanner(document.getText());
let token = scanner.scan();
let ranges: FoldingRange[] = [];
let stack: { startLine: number, tagName: string }[] = [];
let lastTagName = null;
let prevStart = -1;
function addRange(range: FoldingRange) {
ranges.push(range);
prevStart = range.startLine;
}
while (token !== TokenType.EOS) {
switch (token) {
case TokenType.StartTag: {
let tagName = scanner.getTokenText();
let startLine = document.positionAt(scanner.getTokenOffset()).line;
stack.push({ startLine, tagName });
lastTagName = tagName;
break;
}
case TokenType.EndTag: {
lastTagName = scanner.getTokenText();
break;
}
case TokenType.StartTagClose:
if (!lastTagName || !isEmptyElement(lastTagName)) {
break;
}
// fallthrough
case TokenType.EndTagClose:
case TokenType.StartTagSelfClose: {
let i = stack.length - 1;
while (i >= 0 && stack[i].tagName !== lastTagName) {
i--;
}
if (i >= 0) {
let stackElement = stack[i];
stack.length = i;
let line = document.positionAt(scanner.getTokenOffset()).line;
let startLine = stackElement.startLine;
let endLine = line - 1;
if (endLine > startLine && prevStart !== startLine) {
addRange({ startLine, endLine });
}
}
break;
}
case TokenType.Comment: {
let startLine = document.positionAt(scanner.getTokenOffset()).line;
let text = scanner.getTokenText();
let m = text.match(/^\s*#(region\b)|(endregion\b)/);
if (m) {
if (m[1]) { // start pattern match
stack.push({ startLine, tagName: '' }); // empty tagName marks region
} else {
let i = stack.length - 1;
while (i >= 0 && stack[i].tagName.length) {
i--;
}
if (i >= 0) {
let stackElement = stack[i];
stack.length = i;
let endLine = startLine;
startLine = stackElement.startLine;
if (endLine > startLine && prevStart !== startLine) {
addRange({ startLine, endLine, type: FoldingRangeType.Region });
}
}
}
} else {
let endLine = document.positionAt(scanner.getTokenOffset() + scanner.getTokenLength()).line;
if (startLine < endLine) {
addRange({ startLine, endLine, type: FoldingRangeType.Comment });
}
}
break;
}
}
token = scanner.scan();
}
return ranges;
}