Shanedell commented on code in PR #117: URL: https://github.com/apache/daffodil-vscode/pull/117#discussion_r886190109
########## src/language/dfdl.ts: ########## @@ -0,0 +1,1328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as vscode from 'vscode' + +export function activate(context: vscode.ExtensionContext) { + const elementCompletionProvider = + vscode.languages.registerCompletionItemProvider('dfdl', { + provideCompletionItems( + document: vscode.TextDocument, + position: vscode.Position, + token: vscode.CancellationToken, + context: vscode.CompletionContext + ) { + const wholeLine = document + .lineAt(position) + .text.substr(0, position.character) + if (checkBraceOpen(document, position)) { + console.log('in elementCompletionProvider - brace is showing open') + return undefined + } + if (wholeLine.includes('Variable')) { + return undefined + } + + var definedVariables = getDefinedVariables(document) + // a simple completion item which inserts `Hello World!` + //const simpleCompletion = new vscode.CompletionItem('Hello World!'); + + // a completion item that inserts its text as snippet, + // the `insertText`-property is a `SnippetString` which will be + // honored by the editor. + const xmlVersion = new vscode.CompletionItem('xml version') + xmlVersion.insertText = new vscode.SnippetString( + '<?xml version="1.0" encoding="UTF-8"?>\n$0' + ) + + const xsSchema = new vscode.CompletionItem('xs:schema') + xsSchema.insertText = new vscode.SnippetString( + '<xs:schema xmlns:xs="http://www.w3.org2001/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>' + ) + + const xsElement = new vscode.CompletionItem('xs:element name') + xsElement.insertText = new vscode.SnippetString( + '<xs:element name="$1"$0' + ) + //xsElement.insertText = new vscode.SnippetString('<xs:element name="$1"$0>\n\n</xs:element>'); + xsElement.documentation = new vscode.MarkdownString('A new dfdl item') + + const xsElementRef = new vscode.CompletionItem('xs:element ref') + xsElementRef.insertText = new vscode.SnippetString( + '<xs:element ref="$1"$0' + ) + //xsElementRef.insertText = new vscode.SnippetString('<xs:element ref="$1"$0>\n\n</xs:element>'); + xsElementRef.documentation = new vscode.MarkdownString( + 'A new dfdl reference to an item' + ) + + const xsGroup = new vscode.CompletionItem('xs:group name') + xsGroup.insertText = new vscode.SnippetString( + '<xs:group name = "$1">\n\t$0\n</xs:group>' + ) + //xsGroup.documentation = new vscode.MarkdownString(''); + + const xsGroupRef = new vscode.CompletionItem('xs:group ref') + xsGroupRef.insertText = new vscode.SnippetString('<xs:group ref="$1"$0') + //xsGroupRef.insertText = new vscode.SnippetString('<xs:group ref="$1"$0>\n\n</xs:element>'); + xsGroupRef.documentation = new vscode.MarkdownString( + 'A new dfdl reference to an item' + ) + + const dfdlAssert = new vscode.CompletionItem('dfdl:assert') + dfdlAssert.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t<dfdl:assert>"<$1>"</dfdl:assert>\n\t</xs:appinfo>\n</xs:annotation>$0' + ) + dfdlAssert.documentation = new vscode.MarkdownString( + 'dfdl assertion test' + ) + + const dfdlDiscriminator = new vscode.CompletionItem( + 'dfdL:discriminator' + ) + dfdlDiscriminator.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t<dfdl:discriminator test="{$1}"/>\n\t</xs:appinfo>\n</xs:annotation>$0' + ) + dfdlDiscriminator.documentation = new vscode.MarkdownString( + 'dfdl discriminator test' + ) + + const dfdlHiddenGroupRef = new vscode.CompletionItem( + 'dfdl:hiddenGroupRef' + ) + dfdlHiddenGroupRef.insertText = new vscode.SnippetString( + '<xs:sequence dfdl:hiddenGroupRef="$1"/>\n$0' + ) + //dfdlHiddenGroupRef.documentation = new vscode.MarkdownString('Hidden group reference'); + + const defaultDfdlFormat = new vscode.CompletionItem('dfdl:format') + defaultDfdlFormat.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t<dfdl:format alignmentUnits=${1|"bits","bytes"|}\n\t\t\tlengthUnits=${2|"bits","bytes","characters"|}\n\t\t\trepresentation=${3|"binary","text"|}\n\t\t\tbinaryNumberRep=${4|"binary","packed","bcd","ibm4690Packed"|}\n\t\t\tbyteOrder=${5|"bigEndian","littleEndian"|}\n\t\t\tbitOrder=${6|"mostSignificantBitFirst","leastSignificantBitFirst"|}\n\t\t\tlengthKind=${7|"explicit","delimited","implicit","prefixed","pattern","endOfParent"|}\n\t\t\talignment=${8|"1","implicit"|}\n\t\t\tencodingErrorPolicy=${9|"error","replace"|}\n\t\t\tbinaryFloatRep=${10|"ieee","ibm390Hex"|}\n\t\t\tcalendarPatternKind=${11|"explicit","implicit"|}\n\t\t\tdocumentFinalTerminatorCanBeMissing=${12|"yes","no"|}\n\t\t\temptyValueDelimiterPolicy=${13|"initiator","terminator","both","none"|}\n\t\t\tescapeSchemeRef="$14"\n\t\t\tfillByte="$15"\n\t\t\tfloating=${16|"no","yes"|}\n\t\t\tignoreCase=${17|"no","yes"|}\n\t\t\tinitiatedContent=${18 |"no","yes"|}\n\t\t\tinitiator="$19"\n\t\t\tleadingSkip="$20"\n\t\t\tseparator="$21"\n\t\t\tseparatorSuppressionPolicy=${22|"anyEmpty","never","trailingEmpty","trailingEmptyStrict"|}\n\t\t\toutputNewLine=${23|"%CR;","%LF;","%NEL;","%LS;","%CR;%LF;"|}\n\t\t\ttextStandardZeroRep="0$24"\n\t\t\ttextStandardInfinityRep="Inf$25"\n\t\t\ttextStandardExponentRep="E$26"\n\t\t\ttextStandardNaNRep="NaN$27"\n\t\t\ttextNumberPattern="#,##0.###;-#,##0.###$28"\n\t\t\ttextNumberRounding=${29|"explicit","pattern"|}\n\t\t\ttextNumberRoundingMode=${30|"roundCeiling","roundFloor","roundDown","roundUp","roundHalfEven","roundHalfDown","roundHalfUp","roundUnnecessary"|}\n\t\t\ttextNumberRoundingIncrement="0$31"\n\t\t\ttextStandardGroupingSeparator=",$32"\n\t\t\tseparatorPosition=${33|"infix","postFix","prefix"|}\n\t\t\tsequenceKind=${34|"ordered","unordered"|}\n\t\t\tterminator="$35"\n\t\t\ttextBidi=${36|"no","yes"|}"\n\t\t\ttextNumberCheckPolicy=${37|"lax","strict"|}\n\t\t\ttextNumberRep=${38|"standard"," zoned"|}\n\t\t\ttextOutputMinLength="0$39"\n\t\t\ttextPadKind=${40|"none","padChar"|}\n\t\t\ttextStandardBase="10$41"\n\t\t\ttextTrimKind=${42|"none","padChar"|}\n\t\t\ttrailingSkip="0$43"\n\t\t\ttruncateSpecifiedLengthString=${44|"no","yes"|}\n\t\t\tutf16Width=${45|"fixed","variable"|}\n\t\t\tencoding=${46|"US-ASCII","ASCII","UTF-8","UTF-16","UTF-16BE","UTF-16LE","ISO-8859-1"|}\n\t\t\tnilKind=${47|"literalCharacter","literalValue","logicalValue"|}\n\t\t\tnilValueDelimiterPolicy=${48|"initiator","terminator","both","none"|}\n\t\t\toccursCountKind=${49|"expression","fixed","implicit","parsed","stopValue"|}\n\t\t\tchoiceLengthKind=${50|"explicit","implicit"|}/>\n\t</xs:appinfo>\n</xs:annotation>\n$0' + ) + + //annotationBlock.command = { command: 'editor.action.triggerSuggest', title: 'trigger annotation completion' }; + //appinfoBlock.command = { command: 'editor.action.triggerSuggest', title: 'trigger appinfo completion' }; + const annotationBlock = new vscode.CompletionItem('xs:annotation') + //annotationBlock.kind = vscode.CompletionItemKind.Keyword; + annotationBlock.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t$0\n\t</xs:appinfo>\n</xs:annotation>' + ) + const appinfoBlock = new vscode.CompletionItem('xs:appinfo') + //appinfoBlock.kind = vscode.CompletionItemKind.Keyword; + appinfoBlock.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t$0\n\t</xs:appinfo>\n</xs:annotation>' + ) + + const xsComplexType = new vscode.CompletionItem('xs:complexType') + //xsComplexType.insertText = new vscode.SnippetString('<xs:complexType>\n$0'); + xsComplexType.insertText = new vscode.SnippetString( + '<xs:complexType>\n\t$0\n</xs:complexType>' + ) + xsComplexType.documentation = new vscode.MarkdownString( + 'Starts a complex type definition' + ) + + const xsComplexTypeName = new vscode.CompletionItem( + 'xs:complexType name=' + ) + xsComplexTypeName.insertText = new vscode.SnippetString( + '<xs:complexType Name="$1">\n\t$0\n</xs:complexType>' + ) + xsComplexTypeName.documentation = new vscode.MarkdownString( + 'Starts a complex type definition' + ) + + const xsSimpleType = new vscode.CompletionItem('xs:simpleType') + xsSimpleType.insertText = new vscode.SnippetString( + '<xs:simpleType>\n\t$0\n</xs:simpleType>' + ) + xsSimpleType.documentation = new vscode.MarkdownString( + 'Starts a complex type definition' + ) + + const xsSimpleTypeName = new vscode.CompletionItem( + 'xs:simpleType name=' + ) + xsSimpleTypeName.insertText = new vscode.SnippetString( + '<xs:simpleType Name="$1"$0' + ) + xsSimpleTypeName.documentation = new vscode.MarkdownString( + 'Starts a complex type definition' + ) + + const xsSequence = new vscode.CompletionItem('xs:sequence') + //xsSequence.insertText = new vscode.SnippetString('<xs:sequence>\n$0'); + xsSequence.insertText = new vscode.SnippetString('<xs:sequence') + //xsSequence.documentation = new vscode.MarkdownString(""); + + const xsChoice = new vscode.CompletionItem('xs:choice') + xsChoice.insertText = new vscode.SnippetString( + '<xs:choice$1>\n\t$0\n</xs:choice>' + ) + //xsChoice.documentation = new vscode.MarkdownString(""); + + const dfdlDefineVariable = new vscode.CompletionItem( + 'dfdl:defineVariable' + ) + dfdlDefineVariable.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t<dfdl:defineVariable name="$1"$0' + ) + //dfdlDefineVariable.documentation = new vscode.MarkdownString(""); + + const dfdlSetVariable = new vscode.CompletionItem('dfdl:setVariable') + dfdlSetVariable.insertText = new vscode.SnippetString( + '<xs:annotation>\n\t<xs:appinfo source="http://www.ogf.org/dfdl/">\n\t\t<dfdl:setVariable ref="${1|' + + definedVariables + + '|}"$0' + ) + + // return all completion items as array + return [ + xmlVersion, + xsSchema, + xsElement, + xsElementRef, + xsGroup, + xsGroupRef, + dfdlAssert, + dfdlDiscriminator, + dfdlHiddenGroupRef, + defaultDfdlFormat, + annotationBlock, + appinfoBlock, + xsComplexType, + xsComplexTypeName, + xsSimpleType, + xsSimpleTypeName, + xsSequence, + xsChoice, + dfdlDefineVariable, + dfdlSetVariable, + ] + }, + }) + + const attributeCompletionProvider = + vscode.languages.registerCompletionItemProvider( + { language: 'dfdl' }, + { + provideCompletionItems( + document: vscode.TextDocument, + position: vscode.Position + ) { + const wholeLine = document + .lineAt(position) + .text.substr(0, position.character) + var nearestOpenItem = nearestOpen(document, position) + //console.log("in attributeCOmpletionProvider"); + if ( + !checkBraceOpen(document, position) && + !wholeLine.includes('assert') && + !nearestOpenItem.includes('none') + ) { + if (nearestOpenItem.includes('element')) { + var preVal = '' + if (!wholeLine.includes('xs:element')) { + if (lineCount(document, position) === 1) { + preVal = '\t' + } else { + preVal = '' + } + } + var additionalItems = getDefinedTypes(document) + const dfdlDefineFormat = new vscode.CompletionItem( + 'dfdl:defineFormat' + ) + dfdlDefineFormat.insertText = new vscode.SnippetString( + preVal + + '<dfdl:defineFormat name="$1" >\n\t$2\n</dfdl:defineFormat>\n$0' + ) + dfdlDefineFormat.documentation = new vscode.MarkdownString( + 'dfdl format name and configuration' + ) + + const dfdlDefineEscape = new vscode.CompletionItem( + 'dfdl:defineEscapeScheme' + ) + dfdlDefineEscape.insertText = new vscode.SnippetString( + preVal + + '<dfdl:defineEscapeScheme name=$1 >\n\t$0,/dfdl:defineEscapeScheme>\n' + ) + dfdlDefineEscape.documentation = new vscode.MarkdownString( + 'dfdl escape character definition' + ) + + const xmlType = new vscode.CompletionItem('type=') + xmlType.insertText = new vscode.SnippetString( + preVal + + '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' + ) + xmlType.documentation = new vscode.MarkdownString( + 'attribute to specify a simple type element type' + ) + + const minOccurs = new vscode.CompletionItem('minOccurs=') + minOccurs.insertText = new vscode.SnippetString( + preVal + 'minOccurs="${1|0,1|}"$0' + ) + minOccurs.documentation = new vscode.MarkdownString( + 'mininum number of times element will occur' + ) + + const maxOccurs = new vscode.CompletionItem('maxOccurs=') + maxOccurs.insertText = new vscode.SnippetString( + preVal + 'maxOccurs="${1|0,1,unbounded|}"$0' + ) + maxOccurs.documentation = new vscode.MarkdownString( + 'maximum number of times element will occur' + ) + + const dfdlOccursCount = new vscode.CompletionItem( + 'dfdl:occursCount=' + ) + dfdlOccursCount.insertText = new vscode.SnippetString( + preVal + 'dfdl:occursCount="$1"$0' + ) + dfdlOccursCount.documentation = new vscode.MarkdownString( + 'dfdl:occursCount property takes an expression which commonly looks in the Infoset via an expression, to obtain the count from another element.' + ) Review Comment: I am currently working on this fix. As well as splitting the different providers into separate files to keep everything more organized and not to much code in one place -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
