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


TypeScript TextDocument.offsetAt方法代码示例

本文整理汇总了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
  };
}
开发者ID:tiravata,项目名称:vetur,代码行数:28,代码来源:completion-item.ts

示例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;
	});
开发者ID:elibarzilay,项目名称:vscode,代码行数:7,代码来源:formatting.test.ts

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

示例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;
	}
开发者ID:Microsoft,项目名称:vscode-css-languageservice,代码行数:30,代码来源:cssSelectionRange.ts

示例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';
}
开发者ID:cryptobuks,项目名称:tandem,代码行数:13,代码来源:embeddedSupport.ts

示例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);
 }
开发者ID:cryptobuks,项目名称:tandem,代码行数:13,代码来源:hover-test-util.ts

示例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;
}
开发者ID:cryptobuks,项目名称:tandem,代码行数:39,代码来源:embeddedSupport.ts

示例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;
}
开发者ID:sameer-coder,项目名称:vscode,代码行数:23,代码来源:edits.ts

示例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);
	}

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


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