当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript TextDocument.positionAt方法代码示例

本文整理汇总了TypeScript中vscode-languageserver-types.TextDocument.positionAt方法的典型用法代码示例。如果您正苦于以下问题:TypeScript TextDocument.positionAt方法的具体用法?TypeScript TextDocument.positionAt怎么用?TypeScript TextDocument.positionAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vscode-languageserver-types.TextDocument的用法示例。


在下文中一共展示了TextDocument.positionAt方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: 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 };
}
开发者ID:tiravata,项目名称:vetur,代码行数:13,代码来源:htmlFormat.ts

示例2: 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: 'vue'
        });
      }
      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: 'vue'
    });
  }
  return result;
}
开发者ID:tiravata,项目名称:vetur,代码行数:39,代码来源:embeddedSupport.ts

示例3: getAllRegions

function getAllRegions(doc: TextDocument) {
  const startPos = doc.positionAt(0);
  const endPos = doc.positionAt(doc.getText().length);
  return getDocumentRegions(doc).getLanguageRanges(Range.create(startPos, endPos));
}
开发者ID:tiravata,项目名称:vetur,代码行数:5,代码来源:region.test.ts

示例4: format

export function format(languageModes: LanguageModes, document: TextDocument, formatRange: Range, formattingOptions: FormattingOptions, settings: Settings, enabledModes: { [mode: string]: boolean }) {
	let result: TextEdit[] = [];

	let endPos = formatRange.end;
	let endOffset = document.offsetAt(endPos);
	let content = document.getText();
	if (endPos.character === 0 && endPos.line > 0 && endOffset !== content.length) {
		// if selection ends after a new line, exclude that new line
		let prevLineStart = document.offsetAt(Position.create(endPos.line - 1, 0));
		while (isEOL(content, endOffset - 1) && endOffset > prevLineStart) {
			endOffset--;
		}
		formatRange = Range.create(formatRange.start, document.positionAt(endOffset));
	}


	// 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

	// make sure we start in html
	let allRanges = languageModes.getModesInRange(document, formatRange);
	let i = 0;
	let startPos = formatRange.start;
	while (i < allRanges.length && allRanges[i].mode.getId() !== 'html') {
		let range = allRanges[i];
		if (!range.attributeValue && range.mode.format) {
			let edits = range.mode.format(document, Range.create(startPos, range.end), formattingOptions, settings);
			pushAll(result, edits);
		}
		startPos = range.end;
		i++;
	}
	if (i === allRanges.length) {
		return result;
	}
	// modify the range
	formatRange = Range.create(startPos, formatRange.end);

	// perform a html format and apply changes to a new document
	let htmlMode = languageModes.getMode('html')!;
	let htmlEdits = htmlMode.format!(document, formatRange, formattingOptions, settings);
	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, settings);
				for (let edit of edits) {
					embeddedEdits.push(edit);
				}
			}
		}

		if (embeddedEdits.length === 0) {
			pushAll(result, htmlEdits);
			return result;
		}

		// 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);

		result.push(TextEdit.replace(formatRange, resultReplaceText));
		return result;
	} finally {
		languageModes.onDocumentRemoved(newDocument);
	}

}
开发者ID:SeanKilleen,项目名称:vscode,代码行数:81,代码来源:formatting.ts

示例5: doHover

export function doHover(
  document: TextDocument,
  position: Position,
  htmlDocument: HTMLDocument,
  tagProviders: IHTMLTagProvider[]
): Hover {
  const offset = document.offsetAt(position);
  const node = htmlDocument.findNodeAt(offset);
  if (!node || !node.tag) {
    return NULL_HOVER;
  }
  function getTagHover(tag: string, range: Range, open: boolean): Hover {
    tag = tag.toLowerCase();
    for (const provider of tagProviders) {
      let hover: Hover | null = null;
      provider.collectTags((t, label) => {
        if (t === tag) {
          const tagLabel = open ? '<' + tag + '>' : '</' + tag + '>';
          hover = { contents: [{ language: 'html', value: tagLabel }, MarkedString.fromPlainText(label)], range };
        }
      });
      if (hover) {
        return hover;
      }
    }
    return NULL_HOVER;
  }

  function getAttributeHover(tag: string, attribute: string, range: Range): Hover {
    tag = tag.toLowerCase();
    let hover: Hover = NULL_HOVER;
    for (const provider of tagProviders) {
      provider.collectAttributes(tag, (attr, type, documentation) => {
        if (attribute !== attr) {
          return;
        }
        const contents = [documentation ? MarkedString.fromPlainText(documentation) : `No doc for ${attr}`];
        hover = { contents, range };
      });
    }
    return hover;
  }

  const inEndTag = node.endTagStart && offset >= node.endTagStart; // <html></ht|ml>
  const startOffset = inEndTag ? node.endTagStart : node.start;
  const scanner = createScanner(document.getText(), startOffset);
  let token = scanner.scan();

  function shouldAdvance() {
    if (token === TokenType.EOS) {
      return false;
    }
    const tokenEnd = scanner.getTokenEnd();
    if (tokenEnd < offset) {
      return true;
    }

    if (tokenEnd === offset) {
      return TRIVIAL_TOKEN.includes(token);
    }
    return false;
  }

  while (shouldAdvance()) {
    token = scanner.scan();
  }

  if (offset > scanner.getTokenEnd()) {
    return NULL_HOVER;
  }
  const tagRange = {
    start: document.positionAt(scanner.getTokenOffset()),
    end: document.positionAt(scanner.getTokenEnd())
  };
  switch (token) {
    case TokenType.StartTag:
      return getTagHover(node.tag, tagRange, true);
    case TokenType.EndTag:
      return getTagHover(node.tag, tagRange, false);
    case TokenType.AttributeName:
      // TODO: treat : as special bind
      const attribute = scanner.getTokenText().replace(/^:/, '');
      return getAttributeHover(node.tag, attribute, tagRange);
  }

  return NULL_HOVER;
}
开发者ID:tiravata,项目名称:vetur,代码行数:87,代码来源:htmlHover.ts

示例6: getReplaceRange

 function getReplaceRange(replaceStart: number, replaceEnd: number = offset): Range {
   if (replaceStart > offset) {
     replaceStart = offset;
   }
   return { start: document.positionAt(replaceStart), end: document.positionAt(replaceEnd) };
 }
开发者ID:tiravata,项目名称:vetur,代码行数:6,代码来源:htmlCompletion.ts

示例7: findDefinition

export function findDefinition(
  document: TextDocument,
  position: Position,
  htmlDocument: HTMLDocument,
  componentInfos: ComponentInfo[]
): Definition {
  const offset = document.offsetAt(position);
  const node = htmlDocument.findNodeAt(offset);
  if (!node || !node.tag) {
    return [];
  }
  function getTagDefinition(tag: string, range: Range, open: boolean): Definition {
    tag = tag.toLowerCase();
    for (const comp of componentInfos) {
      if (tag === comp.name) {
        return comp.definition || [];
      }
    }
    return [];
  }

  const inEndTag = node.endTagStart && offset >= node.endTagStart; // <html></ht|ml>
  const startOffset = inEndTag ? node.endTagStart : node.start;
  const scanner = createScanner(document.getText(), startOffset);
  let token = scanner.scan();

  function shouldAdvance() {
    if (token === TokenType.EOS) {
      return false;
    }
    const tokenEnd = scanner.getTokenEnd();
    if (tokenEnd < offset) {
      return true;
    }

    if (tokenEnd === offset) {
      return TRIVIAL_TOKEN.includes(token);
    }
    return false;
  }

  while (shouldAdvance()) {
    token = scanner.scan();
  }

  if (offset > scanner.getTokenEnd()) {
    return [];
  }
  const tagRange = {
    start: document.positionAt(scanner.getTokenOffset()),
    end: document.positionAt(scanner.getTokenEnd())
  };
  switch (token) {
    case TokenType.StartTag:
      return getTagDefinition(node.tag, tagRange, true);
    case TokenType.EndTag:
      return getTagDefinition(node.tag, tagRange, false);
  }

  return [];
}
开发者ID:cryptobuks,项目名称:tandem,代码行数:61,代码来源:htmlDefinition.ts

示例8: convertRange

function convertRange(document: TextDocument, span: { start: number, length: number }): Range {
	let startPosition = document.positionAt(span.start);
	let endPosition = document.positionAt(span.start + span.length);
	return Range.create(startPosition, endPosition);
}
开发者ID:,项目名称:,代码行数:5,代码来源:

示例9: convertRange

function convertRange(document: TextDocument, span: ts.TextSpan): Range {
  const startPosition = document.positionAt(span.start);
  const endPosition = document.positionAt(span.start + span.length);
  return Range.create(startPosition, endPosition);
}
开发者ID:tiravata,项目名称:vetur,代码行数:5,代码来源:javascript.ts

示例10: getEndLine

	function getEndLine(t: IToken) {
		return document.positionAt(t.offset + t.len).line;
	}
开发者ID:Microsoft,项目名称:vscode-css-languageservice,代码行数:3,代码来源:cssFolding.ts


注:本文中的vscode-languageserver-types.TextDocument.positionAt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。