當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript polymer-analyzer.isPositionInsideRange函數代碼示例

本文整理匯總了TypeScript中polymer-analyzer.isPositionInsideRange函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isPositionInsideRange函數的具體用法?TypeScript isPositionInsideRange怎麽用?TypeScript isPositionInsideRange使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了isPositionInsideRange函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: getAttributeAstLocation

/**
 * If the position is inside of the node's attributes section, return the
 * correct LocationResult.
 */
function getAttributeAstLocation(
    node: parse5.ASTNode,
    position: SourcePosition,
    document: ParsedHtmlDocument,
    location: Parse5Location): AttributesSection|AttributeValue|undefined {
  /**
   * TODO(rictic): upstream to @types the fact that regular locations (not just
   * element locations) can have attrs sometimes.
   */
  const attrs: parse5.AttributesLocationInfo =
      (isElementLocationInfo(location) && location.startTag.attrs) ||
      location['attrs'] || {};

  for (const attrName in attrs) {
    const range = document.sourceRangeForAttribute(node, attrName);
    if (isPositionInsideRange(position, range)) {
      if (isPositionInsideRange(
              position,
              document.sourceRangeForAttributeValue(node, attrName))) {
        return {kind: 'attributeValue', attribute: attrName, element: node};
      }
      return {kind: 'attribute', attribute: attrName, element: node};
    }
  }
}
開發者ID:Polymer,項目名稱:tools,代碼行數:29,代碼來源:ast-from-source-position.ts

示例2: getDocumentContaining

export function getDocumentContaining(
    sourceRange: SourceRange|undefined, document: Document): ParsedDocument|
    undefined {
  if (!sourceRange) {
    return undefined;
  }
  let mostSpecificDocument: undefined|Document = undefined;
  for (const doc of document.getFeatures({kind: 'document'})) {
    if (isPositionInsideRange(sourceRange.start, doc.sourceRange)) {
      if (!mostSpecificDocument ||
          isPositionInsideRange(
              doc.sourceRange!.start, mostSpecificDocument.sourceRange)) {
        mostSpecificDocument = doc;
      }
    }
  }
  mostSpecificDocument = mostSpecificDocument || document;
  return mostSpecificDocument.parsedDocument;
}
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:19,代碼來源:util.ts

示例3: addImportMetaToElements

  /**
   * Adds a static importMeta property to Polymer elements.
   */
  private addImportMetaToElements(program: Program, scriptDocument: Document) {
    const elements = scriptDocument.getFeatures({'kind': 'polymer-element'});

    for (const element of elements) {
      // This is an analyzer wart. There's no way to avoid getting features
      // from the containing document when querying an inline document. Filed
      // as https://github.com/Polymer/polymer-analyzer/issues/712
      if (element.sourceRange === undefined ||
          !isPositionInsideRange(
              element.sourceRange.start, scriptDocument.sourceRange)) {
        continue;
      }

      const nodePath = getNodePathInProgram(program, element.astNode);

      if (nodePath === undefined) {
        console.warn(
            new Warning({
              code: 'not-found',
              message: `Can't find recast node for element ${element.tagName}`,
              parsedDocument: this.document.parsedDocument,
              severity: Severity.WARNING,
              sourceRange: element.sourceRange!
            }).toString());
        continue;
      }

      const importMeta = jsc.memberExpression(
          jsc.identifier('import'), jsc.identifier('meta'));

      const node = nodePath.node;
      if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
        // A Polymer 2.0 class-based element
        const getter = jsc.methodDefinition(
            'get',
            jsc.identifier('importMeta'),
            jsc.functionExpression(
                null,
                [],
                jsc.blockStatement([jsc.returnStatement(importMeta)])),
            true);
        node.body.body.splice(0, 0, getter);
      } else if (node.type === 'CallExpression') {
        // A Polymer hybrid/legacy factory function element
        const arg = node.arguments[0];
        if (arg && arg.type === 'ObjectExpression') {
          arg.properties.unshift(
              jsc.property('init', jsc.identifier('importMeta'), importMeta));
        }
      } else {
        console.error(`Internal Error, Class or CallExpression expected, got ${
            node.type}`);
      }
    }
  }
開發者ID:,項目名稱:,代碼行數:58,代碼來源:

示例4: getAncestorDomModuleForElement

 private getAncestorDomModuleForElement(
     document: Document, element: dom5.Node) {
   const parsedDocument = document.parsedDocument;
   if (!(parsedDocument instanceof ParsedHtmlDocument)) {
     return;
   }
   const elementSourcePosition =
       parsedDocument.sourceRangeForNode(element)!.start;
   const domModules =
       document.getFeatures({kind: 'dom-module', imported: false});
   for (const domModule of domModules) {
     if (isPositionInsideRange(
             elementSourcePosition,
             parsedDocument.sourceRangeForNode(domModule.node))) {
       return domModule;
     }
   }
 }
開發者ID:Polymer,項目名稱:tools,代碼行數:18,代碼來源:auto-completer.ts

示例5: getCssAstLocationForPosition

export function getCssAstLocationForPosition(
    document: ParsedCssDocument,
    position: SourcePosition): shadyCssParser.Node {
  let closest = document.ast;
  while (true) {
    let containingChild = undefined;
    for (const child of getChildren(closest)) {
      const sourceRange = document.sourceRangeForNode(child);
      if (!sourceRange) {
        continue;
      }
      if (isPositionInsideRange(position, sourceRange)) {
        containingChild = child;
        break;
      }
    }
    if (containingChild === undefined) {
      break;
    }
    closest = containingChild;
  }
  return closest;
}
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:23,代碼來源:ast-from-source-position.ts

示例6: internalGetAstLocationForPosition

function internalGetAstLocationForPosition(
    node: parse5.ASTNode, position: SourcePosition,
    document: ParsedHtmlDocument): undefined|HtmlAstLocation {
  const sourceRange = document.sourceRangeForNode(node);
  const location = node.__location;

  /**
   * An HTML5 parser must hallucinate certain nodes, even if they don't exist
   * in the original source text. e.g. <html> or <body>. So we might have
   * elements that have no sourceRange (because they don't exist in the text)
   * but they do have children that do. So we should check those children.
   */
  if (!(sourceRange && location)) {
    return findLocationInChildren(node, position, document);
  }

  if (!isPositionInsideRange(position, sourceRange)) {
    // definitively not in this node or any of its children
    return;
  }

  const locationInChildren = findLocationInChildren(node, position, document);
  if (locationInChildren) {
    return locationInChildren;
  }

  const attributeAstLocation =
      getAttributeAstLocation(node, position, document, location);
  if (attributeAstLocation) {
    return attributeAstLocation;
  }

  const startTagRange = document.sourceRangeForStartTag(node);
  const endTagRange = document.sourceRangeForEndTag(node);

  // If we're in the end tag... we're in the end tag.
  if (isPositionInsideRange(position, endTagRange, false)) {
    return {kind: 'endTag', element: node};
  }

  if (startTagRange && isPositionInsideRange(position, startTagRange, false)) {
    if (position.line === startTagRange.start.line) {
      // If the cursor is in the "<my-elem" part of the start tag.
      if (position.column <=
          startTagRange.start.column + (node.tagName || '').length + 1) {
        return {kind: 'tagName', element: node};
      }
    }
    // Otherwise we're in the start tag, but not in the tag name or any
    // particular attribute, but definitely in the attributes section.
    return {kind: 'attribute', attribute: null, element: node};
  }

  // The edges of a comment aren't part of the comment.
  if (parse5.treeAdapters.default.isCommentNode(node) &&
      isPositionInsideRange(position, sourceRange, false)) {
    return {kind: 'comment', commentNode: node};
  }

  if (parse5.treeAdapters.default.isTextNode(node)) {
    const parent = node.parentNode;
    if (parent && parent.tagName === 'script') {
      return {kind: 'scriptTagContents', textNode: node};
    }
    if (parent && parent.tagName === 'style') {
      return {kind: 'styleTagContents', textNode: node};
    }
    return {kind: 'text', textNode: node};
  }


  if (isPositionInsideRange(position, sourceRange, false)) {
    /**
     * This is tricky. Consider the position inside an empty element, i.e.
     * here:
     *    <script>|</script>.
     *
     * You can be between the start and end tags, but there won't be a text
     * node to attach to, but if you started typeing, there would be, so we
     * want to treat you as though you are.
     */
    if (startTagRange && endTagRange &&
        comparePositionAndRange(position, startTagRange, false) > 0 &&
        comparePositionAndRange(position, endTagRange, false) < 0) {
      if (node.tagName === 'script') {
        return {kind: 'scriptTagContents'};
      }
      if (node.tagName === 'style') {
        return {kind: 'styleTagContents'};
      }
      return {kind: 'text'};
    }

    /**
     * Ok, we're in this node, we're not in any of its children, but we're not
     * obviously in any attribute, tagname, start tag, or end tag. We might be
     * part of a unclosed tag in a mostly empty document. parse5 doesn't give
     * us much explicit signal in this case, but we can kinda infer it from the
     * tagName.
     */
//.........這裏部分代碼省略.........
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:101,代碼來源:ast-from-source-position.ts

示例7: getFeatureForAstLocation

  /**
   * Given an AstLocation, return a high level feature.
   */
  getFeatureForAstLocation(
      astWrapperLocation: AstLocation, position: SourcePosition): FoundFeature
      |undefined {
    if (astWrapperLocation.language === 'css') {
      const {document, node} = astWrapperLocation;
      const cssFeatures =
          new Set<CssCustomPropertyAssignment|CssCustomPropertyUse>([
            ...document.getFeatures({kind: 'css-custom-property-assignment'}),
            ...document.getFeatures({kind: 'css-custom-property-use'})
          ]);
      const nodeRange = document.parsedDocument.sourceRangeForNode(node);
      if (!nodeRange) {
        return;
      }
      for (const feature of cssFeatures) {
        if (isContained(feature.sourceRange, nodeRange)) {
          return {document, feature};
        }
      }
      return;
    }
    const document = astWrapperLocation.document;
    const htmlLocation = astWrapperLocation.node;
    if (htmlLocation.kind === 'tagName') {
      const feature = getOnly(document.getFeatures({
        kind: 'element',
        id: htmlLocation.element.nodeName,
        imported: true,
        externalPackages: true
      }));
      if (feature) {
        return {document, feature};
      }
      return;
    } else if (htmlLocation.kind === 'attribute') {
      const elements = document.getFeatures({
        kind: 'element',
        id: htmlLocation.element.nodeName,
        imported: true,
        externalPackages: true
      });
      if (elements.size === 0) {
        return;
      }

      const feature = concatMap(elements, (el) => el.attributes.values())
                          .find(at => at.name === htmlLocation.attribute);
      if (feature) {
        return {document, feature};
      }
      return;
    } else if (
        htmlLocation.kind === 'attributeValue' ||
        htmlLocation.kind === 'text') {
      const domModules = document.getFeatures({kind: 'dom-module'});
      for (const domModule of domModules) {
        if (!domModule.id) {
          continue;
        }
        const elements = document.getFeatures({
          kind: 'polymer-element',
          id: domModule.id,
          imported: true,
          externalPackages: true
        });
        if (elements.size !== 1) {
          continue;
        }
        const element = elements.values().next().value!;
        if (isPositionInsideRange(position, domModule.sourceRange)) {
          for (const databinding of domModule.databindings) {
            if (isPositionInsideRange(
                    position, databinding.sourceRange, true)) {
              for (const prop of databinding.properties) {
                if (isPositionInsideRange(position, prop.sourceRange, true)) {
                  return {
                    feature: new DatabindingFeature(
                        element, databinding, prop.name, prop.sourceRange),
                    document
                  };
                }
              }
              return {
                feature: new DatabindingFeature(
                    element, databinding, undefined, undefined),
                document
              };
            }
          }
        }
      }
    }
  }
開發者ID:asdfg9822,項目名稱:polymer-editor-service,代碼行數:96,代碼來源:feature-finder.ts

示例8: isContained

function isContained(inner: SourceRange, outer: SourceRange) {
  return isPositionInsideRange(inner.start, outer, true) &&
      isPositionInsideRange(inner.end, outer, true);
}
開發者ID:asdfg9822,項目名稱:polymer-editor-service,代碼行數:4,代碼來源:feature-finder.ts

示例9: inlineTemplates

  /**
   * Find Polymer element templates in the original HTML. Insert these
   * templates as strings as part of the javascript element declaration.
   */
  private inlineTemplates(program: Program, scriptDocument: Document) {
    const elements = scriptDocument.getFeatures({'kind': 'polymer-element'});
    const claimedDomModules = new Set<parse5.ASTNode>();

    for (const element of elements) {
      // This is an analyzer wart. There's no way to avoid getting features
      // from the containing document when querying an inline document. Filed
      // as https://github.com/Polymer/polymer-analyzer/issues/712
      if (element.sourceRange === undefined ||
          !isPositionInsideRange(
              element.sourceRange.start, scriptDocument.sourceRange)) {
        continue;
      }
      const domModule = element.domModule;
      if (domModule === undefined) {
        continue;
      }
      if (!canDomModuleBeInlined(domModule)) {
        continue;
      }
      claimedDomModules.add(domModule);
      const template = dom5.query(domModule, (e) => e.tagName === 'template');
      if (template === null) {
        continue;
      }

      // It's ok to tag templates with the expression `Polymer.html` without
      // adding an import because `Polymer.html` is re-exported by both
      // polymer.html and polymer-element.html and, crucially, template
      // inlining happens before rewriting references.
      const templateLiteral = jsc.taggedTemplateExpression(
          jsc.memberExpression(
              jsc.identifier('Polymer'), jsc.identifier('html')),
          serializeNodeToTemplateLiteral(
              parse5.treeAdapters.default.getTemplateContent(template)));
      const nodePath = getNodePathInProgram(program, element.astNode);

      if (nodePath === undefined) {
        console.warn(
            new Warning({
              code: 'not-found',
              message: `Can't find recast node for element ${element.tagName}`,
              parsedDocument: this.document.parsedDocument,
              severity: Severity.WARNING,
              sourceRange: element.sourceRange!
            }).toString());
        continue;
      }

      const node = nodePath.node;
      if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
        // A Polymer 2.0 class-based element
        node.body.body.splice(
            0,
            0,
            jsc.methodDefinition(
                'get',
                jsc.identifier('template'),
                jsc.functionExpression(
                    null, [], jsc.blockStatement([jsc.returnStatement(
                                  templateLiteral)])),
                true));
      } else if (node.type === 'CallExpression') {
        // A Polymer hybrid/legacy factory function element
        const arg = node.arguments[0];
        if (arg && arg.type === 'ObjectExpression') {
          arg.properties.unshift(jsc.property(
              'init', jsc.identifier('_template'), templateLiteral));
        }
      } else {
        console.error(`Internal Error, Class or CallExpression expected, got ${
            node.type}`);
      }
    }
    return claimedDomModules;
  }
開發者ID:,項目名稱:,代碼行數:80,代碼來源:


注:本文中的polymer-analyzer.isPositionInsideRange函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。