You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by sh...@apache.org on 2022/10/03 02:20:53 UTC

[daffodil-vscode] branch main updated: Complete the default namespace work

This is an automated email from the ASF dual-hosted git repository.

shanedell pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil-vscode.git


The following commit(s) were added to refs/heads/main by this push:
     new f56b05f  Complete the default namespace work
f56b05f is described below

commit f56b05fa6ad08b8973547820a57a1518f67992c3
Author: Davin Shearer <sc...@gmail.com>
AuthorDate: Wed Sep 28 16:58:51 2022 -0400

    Complete the default namespace work
    
    There were still several instances of the 'xs' namespace that were hard coded.
    
    fixes #120
---
 src/language/providers/attributeCompletion.ts      | 89 +++++++++++++---------
 src/language/providers/elementCompletion.ts        |  2 +-
 .../providers/intellisense/attributeItems.ts       | 11 +--
 src/language/providers/intellisense/commonItems.ts |  3 +-
 .../providers/intellisense/elementItems.ts         | 28 +++----
 src/language/providers/utils.ts                    | 43 ++++++-----
 src/tests/suite/language/items.test.ts             |  6 +-
 7 files changed, 103 insertions(+), 79 deletions(-)

diff --git a/src/language/providers/attributeCompletion.ts b/src/language/providers/attributeCompletion.ts
index bbf60ea..bc4561d 100644
--- a/src/language/providers/attributeCompletion.ts
+++ b/src/language/providers/attributeCompletion.ts
@@ -25,25 +25,29 @@ import {
   checkSequenceOpen,
   checkElementOpen,
   checkSimpleTypeOpen,
+  createCompletionItem,
+  getCommonItems,
+  getXsdNsPrefix,
 } from './utils'
 
 import { attributeCompletion } from './intellisense/attributeItems'
-import { getCommonItems, createCompletionItem } from './utils'
 
 function getCompletionItems(
   itemsToUse: string[],
   preVal: string = '',
-  additionalItems: string = ''
+  additionalItems: string = '',
+  nsPrefix: string
 ) {
   let compItems: vscode.CompletionItem[] = getCommonItems(
     itemsToUse,
     preVal,
-    additionalItems
+    additionalItems,
+    nsPrefix
   )
 
-  attributeCompletion(additionalItems).items.forEach((e) => {
+  attributeCompletion(additionalItems, nsPrefix).items.forEach((e) => {
     if (itemsToUse.includes(e.item)) {
-      const completionItem = createCompletionItem(e, preVal)
+      const completionItem = createCompletionItem(e, preVal, nsPrefix)
       compItems.push(completionItem)
     }
   })
@@ -63,6 +67,7 @@ export function getAttributeCompletionProvider() {
           .lineAt(position)
           .text.substr(0, position.character)
         var nearestOpenItem = nearestOpen(document, position)
+        const nsPrefix = getXsdNsPrefix(document, position)
 
         if (
           !checkBraceOpen(document, position) &&
@@ -71,19 +76,19 @@ export function getAttributeCompletionProvider() {
         ) {
           if (nearestOpenItem.includes('element')) {
             var preVal = ''
-            if (!wholeLine.includes('xs:element')) {
+            if (!wholeLine.includes(nsPrefix + 'element')) {
               if (lineCount(document, position) === 1) {
                 preVal = '\t'
               } else {
                 preVal = ''
               }
             }
-            var additionalItems = getDefinedTypes(document)
+            var additionalItems = getDefinedTypes(document, nsPrefix)
 
             if (
               checkLastItemOpen(document, position) &&
-              (wholeLine.includes('<xs:element name="') ||
-                wholeLine.includes('<xs:element ref="') ||
+              (wholeLine.includes('<' + nsPrefix + 'element name="') ||
+                wholeLine.includes('<' + nsPrefix + 'element ref="') ||
                 checkElementOpen(document, position))
             ) {
               return getCompletionItems(
@@ -111,14 +116,15 @@ export function getAttributeCompletionProvider() {
                   'dfdl:representation',
                 ],
                 preVal,
-                additionalItems
+                additionalItems,
+                nsPrefix
               )
             }
           }
 
           if (nearestOpenItem.includes('sequence')) {
             var preVal = ''
-            if (!wholeLine.includes('xs:sequence')) {
+            if (!wholeLine.includes(nsPrefix + 'sequence')) {
               if (lineCount(document, position) === 1) {
                 preVal = '\t'
               } else {
@@ -128,7 +134,7 @@ export function getAttributeCompletionProvider() {
 
             if (
               checkLastItemOpen(document, position) &&
-              (wholeLine.includes('<xs:sequence') ||
+              (wholeLine.includes('<' + nsPrefix + 'sequence') ||
                 checkSequenceOpen(document, position))
             ) {
               return getCompletionItems(
@@ -139,20 +145,27 @@ export function getAttributeCompletionProvider() {
                   'dfdl:separatorPosition=',
                   'dfdl:separatorSuppressionPolicy',
                 ],
-                preVal
+                preVal,
+                undefined,
+                nsPrefix
               )
             }
           }
 
           if (wholeLine.includes('choice')) {
             if (!wholeLine.includes('>')) {
-              return getCompletionItems([
-                'dfdl:choiceLengthKind=',
-                'dfdl:choiceLength=',
-                'dfdl:intiatedContent=',
-                'dfdl:choiceDispatchKey=',
-                'dfdl:choiceBranchKey=',
-              ])
+              return getCompletionItems(
+                [
+                  'dfdl:choiceLengthKind=',
+                  'dfdl:choiceLength=',
+                  'dfdl:intiatedContent=',
+                  'dfdl:choiceDispatchKey=',
+                  'dfdl:choiceBranchKey=',
+                ],
+                undefined,
+                undefined,
+                nsPrefix
+              )
             }
           }
 
@@ -161,13 +174,18 @@ export function getAttributeCompletionProvider() {
             checkSimpleTypeOpen(document, position)
           ) {
             if (!wholeLine.includes('>')) {
-              return getCompletionItems([
-                'dfdl:length=',
-                'dfdl:lengthKind=',
-                'dfdl:simpleType',
-                'dfdl:simpleType',
-                'xs:restriction',
-              ])
+              return getCompletionItems(
+                [
+                  'dfdl:length=',
+                  'dfdl:lengthKind=',
+                  'dfdl:simpleType',
+                  'dfdl:simpleType',
+                  nsPrefix + 'restriction',
+                ],
+                undefined,
+                undefined,
+                nsPrefix
+              )
             }
           }
 
@@ -180,7 +198,7 @@ export function getAttributeCompletionProvider() {
                 preVal = ''
               }
             }
-            var additionalItems = getDefinedTypes(document)
+            var additionalItems = getDefinedTypes(document, nsPrefix)
 
             var xmlItems = [
               {
@@ -204,9 +222,11 @@ export function getAttributeCompletionProvider() {
                 compItems.push(completionItem)
               })
 
-              getCommonItems(['type='], '', additionalItems).forEach((ci) => {
-                compItems.push(ci)
-              })
+              getCommonItems(['type='], '', additionalItems, nsPrefix).forEach(
+                (ci) => {
+                  compItems.push(ci)
+                }
+              )
 
               return compItems
             }
@@ -239,17 +259,18 @@ export function getAttributeCompletionProvider() {
   )
 }
 
-function getDefinedTypes(document: vscode.TextDocument) {
+function getDefinedTypes(document: vscode.TextDocument, nsPrefix: string) {
   var additionalTypes = ''
   var lineNum = 0
   const lineCount = document.lineCount
+
   while (lineNum !== lineCount) {
     const wholeLine = document
       .lineAt(lineNum)
       .text.substring(0, document.lineAt(lineNum).range.end.character)
     if (
-      wholeLine.includes('xs:simpleType Name=') ||
-      wholeLine.includes('xs:complexType Name=')
+      wholeLine.includes(nsPrefix + 'simpleType Name=') ||
+      wholeLine.includes(nsPrefix + 'complexType Name=')
     ) {
       var startPos = wholeLine.indexOf('"', 0)
       var endPos = wholeLine.indexOf('"', startPos + 1)
diff --git a/src/language/providers/elementCompletion.ts b/src/language/providers/elementCompletion.ts
index 64532e9..94b85cb 100644
--- a/src/language/providers/elementCompletion.ts
+++ b/src/language/providers/elementCompletion.ts
@@ -45,7 +45,7 @@ export function getElementCompletionProvider(dfdlFormatString: string) {
         dfdlFormatString,
         nsPrefix
       ).items.forEach((e) => {
-        const completionItem = createCompletionItem(e, '')
+        const completionItem = createCompletionItem(e, '', nsPrefix)
         compItems.push(completionItem)
       })
 
diff --git a/src/language/providers/intellisense/attributeItems.ts b/src/language/providers/intellisense/attributeItems.ts
index 63be28c..442ac2c 100644
--- a/src/language/providers/intellisense/attributeItems.ts
+++ b/src/language/providers/intellisense/attributeItems.ts
@@ -16,7 +16,7 @@
  */
 
 // prettier-ignore
-export const attributeCompletion = (additionalItems) => {
+export const attributeCompletion = (additionalItems, nsPrefix: string) => {
   return {
     items: [
       {
@@ -32,7 +32,7 @@ export const attributeCompletion = (additionalItems) => {
       {
         item: 'minOccurs=',
         snippetString: 'minOccurs="${1|0,1|}"$0',
-        markdownString: 'mininum number of times element will occur',
+        markdownString: 'minimum number of times element will occur',
       },
       {
         item: 'maxOccurs=',
@@ -160,11 +160,12 @@ export const attributeCompletion = (additionalItems) => {
       },
       {
         item: 'dfdl:simpleType',
-        snippetString: '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\trepresentation="${1|binary,|"\n\t</xs:appinfo>\n</xs:annotation>$0',
+        snippetString: '<' + nsPrefix + 'annotation>\n\t<' + nsPrefix + 'appinfo source="http://www.ogf.org/dfdl/">\n\t\trepresentation="${1|binary,|"\n\t</' + nsPrefix + 'appinfo>\n</' + nsPrefix + 'annotation>$0',
       },
       {
-        item: 'xs:restriction',
-        snippetString: '<xs:restriction base="${1|xs:string,xs:decimal,xs:float,xs:double,xs:integer,xs:nonNegativeInteger,xs:int,xs:unsignedInt,xs:short,xs:unsignedShort,xs:long,xs:unsignedLong,xs:byte,xs:unsignedByte,xs:hexBinary,xs:boolean|}"/>$0',
+        item: nsPrefix + 'restriction',
+        // use the "xs:" prefix for primitive types to differentiate them from custom simple types
+        snippetString: '<' + nsPrefix + 'restriction base="${1|xs:string,xs:decimal,xs:float,xs:double,xs:integer,xs:nonNegativeInteger,xs:int,xs:unsignedInt,xs:short,xs:unsignedShort,xs:long,xs:unsignedLong,xs:byte,xs:unsignedByte,xs:hexBinary,xs:boolean|}"/>$0',
       },
     ],
   }
diff --git a/src/language/providers/intellisense/commonItems.ts b/src/language/providers/intellisense/commonItems.ts
index cf2cd78..cf7ea58 100644
--- a/src/language/providers/intellisense/commonItems.ts
+++ b/src/language/providers/intellisense/commonItems.ts
@@ -16,11 +16,12 @@
  */
 
 // prettier-ignore
-export const commonCompletion = (additionalItems) => {
+export const commonCompletion = (additionalItems, nsPrefix: string) => {
   return {
     items: [
       {
         item: 'type=',
+        // use the "xs:" prefix for primitive types to differentiate them from custom simple types
         snippetString: 'type="${1|xs:string,xs:decimal,xs:float,xs:double,xs:integer,xs:nonNegativeInteger,xs:int,xs:unsignedInt,xs:short,xs:unsignedShort,xs:long,xs:unsignedLong,xs:byte,xs:unsignedByte,xs:hexBinary,xs:boolean' + additionalItems + '|}"$0',
         markdownString: 'an attribute which specifies the type of a simply-typed element',
       },
diff --git a/src/language/providers/intellisense/elementItems.ts b/src/language/providers/intellisense/elementItems.ts
index bbedd1c..1a88c9f 100644
--- a/src/language/providers/intellisense/elementItems.ts
+++ b/src/language/providers/intellisense/elementItems.ts
@@ -24,25 +24,25 @@ export const elementCompletion = (definedVariables, dfdlFormatString, nsPrefix)
         snippetString: '<?xml version="1.0" encoding="UTF-8"?>\n$0',
       },
       {
-        item: 'xs:schema',
-        snippetString: '<xs:schema xmlns:xs="http://www.w3.org/2001/xmlSchema"\n\t\txmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"\n\t\txmlns:daf="urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:ext"\n\t\txmlns:fn="http:/www.w3.org/2005/xpath-functions"\n\t\txmlns:math="www.w3.org/2005/xpath-functions/math" elementFormDefault="qualified">\n$0\n</xs:schema>',
+        item: nsPrefix + 'schema',
+        snippetString: '<' + nsPrefix + 'schema xmlns:xs="http://www.w3.org/2001/xmlSchema"\n\t\txmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"\n\t\txmlns:daf="urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:ext"\n\t\txmlns:fn="http:/www.w3.org/2005/xpath-functions"\n\t\txmlns:math="www.w3.org/2005/xpath-functions/math" elementFormDefault="qualified">\n$0\n</' + nsPrefix + 'schema>',
       },
       {
-        item: 'xs:element name',
+        item: nsPrefix + 'element name',
         snippetString: '<' + nsPrefix + 'element name="$1"$0',
         markdownString: 'A new xs element',
       },
       {
-        item: 'xs:element ref',
+        item: nsPrefix + 'element ref',
         snippetString: '<' + nsPrefix + 'element ref="$1"$0',
         markdownString: 'A new dfdl reference to an item',
       },
       {
-        item: 'xs:group name',
+        item: nsPrefix + 'group name',
         snippetString: '<' + nsPrefix + 'group name = "$1">\n\t$0\n</' + nsPrefix + 'group>',
       },
       {
-        item: 'xs:group ref',
+        item: nsPrefix + 'group ref',
         snippetString: '<' + nsPrefix + 'group ref="$1"$0',
         markdownString: 'A new dfdl reference to an item',
       },
@@ -65,39 +65,39 @@ export const elementCompletion = (definedVariables, dfdlFormatString, nsPrefix)
         snippetString: dfdlFormatString,
       },
       {
-        item: 'xs:annotation',
+        item: nsPrefix + 'annotation',
         snippetString: '<' + nsPrefix + 'annotation>\n\t<' + nsPrefix + 'appinfo source="http://www.ogf.org/dfdl/">\n\t\t$0\n\t</' + nsPrefix + 'appinfo>\n</' + nsPrefix + 'annotation>',
       },
       {
-        item: 'xs:appinfo',
+        item: nsPrefix + 'appinfo',
         snippetString: '<' + nsPrefix + 'annotation>\n\t<' + nsPrefix + 'appinfo source="http://www.ogf.org/dfdl/">\n\t\t$0\n\t</' + nsPrefix + 'appinfo>\n</' + nsPrefix + 'annotation>',
       },
       {
-        item: 'xs:complexType',
+        item: nsPrefix + 'complexType',
         snippetString: '<' + nsPrefix + 'complexType>\n\t$0\n</' + nsPrefix + 'complexType>',
         markdownString: 'Starts a complex type definition',
       },
       {
-        item: 'xs:complexType name=',
+        item: nsPrefix + 'complexType name=',
         snippetString: '<' + nsPrefix + 'complexType Name="$1">\n\t$0\n</' + nsPrefix + 'complexType>',
         markdownString: 'Starts a complex type definition',
       },
       {
-        item: 'xs:simpleType',
+        item: nsPrefix + 'simpleType',
         snippetString: '<' + nsPrefix + 'simpleType>\n\t$0\n</' + nsPrefix + 'simpleType>',
         markdownString: 'Starts a simple type definition',
       },
       {
-        item: 'xs:simpleType name=',
+        item: nsPrefix + 'simpleType name=',
         snippetString: '<' + nsPrefix + 'simpleType Name="$1"$0',
         markdownString: 'Starts a simple type definition',
       },
       {
-        item: 'xs:sequence',
+        item: nsPrefix + 'sequence',
         snippetString: '<' + nsPrefix + 'sequence',
       },
       {
-        item: 'xs:choice',
+        item: nsPrefix + 'choice',
         snippetString: '<' + nsPrefix + 'choice',
       },
       {
diff --git a/src/language/providers/utils.ts b/src/language/providers/utils.ts
index 0f2bc31..dadc0eb 100644
--- a/src/language/providers/utils.ts
+++ b/src/language/providers/utils.ts
@@ -16,9 +16,13 @@
  */
 
 import * as vscode from 'vscode'
+import { commonCompletion } from './intellisense/commonItems'
 
 const schemaPrefixRegEx = new RegExp('</?(|[^ ]+:)schema')
 
+// default namespace in the event that a namespace was not found
+const defaultXsdNsPrefix = 'xs'
+
 // Function to insert snippet to active editor
 export function insertSnippet(snippetString: string, backpos: vscode.Position) {
   vscode.window.activeTextEditor?.insertSnippet(
@@ -38,7 +42,7 @@ export function checkLastItemOpen(
     --lineNum
   }
   const previousLine = document.lineAt(lineNum).text
-  if (
+  return !(
     previousLine.includes('</') ||
     previousLine.includes('/>') ||
     ((wholeLine.includes('element') ||
@@ -47,10 +51,7 @@ export function checkLastItemOpen(
       wholeLine.includes('group') ||
       wholeLine.includes('Variable')) &&
       (wholeLine.includes('</') || wholeLine.includes('/>')))
-  ) {
-    return false
-  }
-  return true
+  )
 }
 
 export function lineCount(
@@ -289,7 +290,7 @@ export function getXsdNsPrefix(
     ++lineNum
   }
   //returns the standard prefix plus a colon in the case of missing schema tag
-  return 'xs:'
+  return defaultXsdNsPrefix + ':'
 }
 
 export function checkBraceOpen(
@@ -339,16 +340,6 @@ export function checkBraceOpen(
   return false
 }
 
-import { commonCompletion } from './intellisense/commonItems'
-
-export const noPreVals: string[] = [
-  'dfdl:choiceBranchKey=',
-  'dfdl:representation',
-  'dfdl:choiceDispatchKey=',
-  'dfdl:simpleType',
-  'xs:restriction',
-]
-
 export function createCompletionItem(
   e:
     | {
@@ -359,12 +350,21 @@ export function createCompletionItem(
     | {
         item: string
         snippetString: string
-        markdownString?: undefined
+        markdownString: undefined
       },
-  preVal: string
+  preVal: string,
+  nsPrefix: string
 ) {
   const completionItem = new vscode.CompletionItem(e.item)
 
+  const noPreVals = [
+    'dfdl:choiceBranchKey=',
+    'dfdl:representation',
+    'dfdl:choiceDispatchKey=',
+    'dfdl:simpleType',
+    nsPrefix + 'restriction',
+  ]
+
   if (preVal !== '' && !noPreVals.includes(e.item)) {
     completionItem.insertText = new vscode.SnippetString(
       preVal + e.snippetString
@@ -383,13 +383,14 @@ export function createCompletionItem(
 export function getCommonItems(
   itemsToUse: string[],
   preVal: string = '',
-  additionalItems: string = ''
+  additionalItems: string = '',
+  nsPrefix: string
 ) {
   let compItems: vscode.CompletionItem[] = []
 
-  commonCompletion(additionalItems).items.forEach((e) => {
+  commonCompletion(additionalItems, nsPrefix).items.forEach((e) => {
     if (itemsToUse.includes(e.item)) {
-      const completionItem = createCompletionItem(e, preVal)
+      const completionItem = createCompletionItem(e, preVal, nsPrefix)
       compItems.push(completionItem)
     }
   })
diff --git a/src/tests/suite/language/items.test.ts b/src/tests/suite/language/items.test.ts
index 55de964..848aaac 100644
--- a/src/tests/suite/language/items.test.ts
+++ b/src/tests/suite/language/items.test.ts
@@ -80,18 +80,18 @@ suite('Items Test Suite', () => {
 
   test('all commonItems available', async () => {
     var itemNames: string[] = []
-    commonCompletion('').items.forEach((r) => itemNames.push(r.item))
+    commonCompletion('', 'xs:').items.forEach((r) => itemNames.push(r.item))
     assert.strictEqual(itemNames.includes('type='), true)
   })
 
   test('all elementItems available', async () => {
-    elementCompletion('', '', '').items.forEach((item) => {
+    elementCompletion('', '', 'xs:').items.forEach((item) => {
       assert.strictEqual(expectedElementItems.includes(item.item), true)
     })
   })
 
   test('all attributeItems available', async () => {
-    attributeCompletion('').items.forEach((item) => {
+    attributeCompletion('', 'xs:').items.forEach((item) => {
       assert.strictEqual(expectedAttributeItems.includes(item.item), true)
     })
   })