當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。