Skip to content

Commit

Permalink
Merge pull request #30 from isc-egabhart/main
Browse files Browse the repository at this point in the history
CodeTidy with JSON linting
  • Loading branch information
isc-tleavitt authored Jan 18, 2023
2 parents 046bcf0 + 63e5f8d commit 2f6c1ea
Show file tree
Hide file tree
Showing 29 changed files with 1,825 additions and 550 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.2] - 2023-01-18

### Fixed
- JSON linting and bracket matching now working
- Fixed preserving indentation of embedded HTML and block comments
- Fixed parsing of custom markers for embedded block open/close brackets
- Removed expansion of "&js<" to "&javascript" and fixed parsing of "&js<"
- Fixed indentation inconsistencies when user inputs mix of spaces and tabs

## [1.1.1] - 2022-12-06

### Fixed
Expand Down
333 changes: 256 additions & 77 deletions cls/pkg/isc/codetidy/Assistant.cls

Large diffs are not rendered by default.

44 changes: 35 additions & 9 deletions cls/pkg/isc/codetidy/CodeTidy.cls
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,12 @@ ClassMethod ParseLine(ByRef last, ByRef this, ByRef next, ByRef conv)

if ..#DEBUG, $increment(%debug) < 1000 write this("Indent"), ":", next("Indent"), ":", multiLineCommand, ":", conv("Line"), !

if ($extract(conv("Line")) = ##class(pkg.isc.codetidy.Utils).GetIndentString()) {
set indentString = ##class(pkg.isc.codetidy.Utils).GetIndentString()
set indented = ($extract(conv("Line"), 1, $length(indentString)) = indentString)
if (indented) && (last("Lang") '= "HTML") {
// Indentation
set conv("Frag") = ##class(pkg.isc.codetidy.Utils).GetIndentString() _ $translate($justify("", this("Indent") + multiLineCommand), $char(32), $char(9))
set conv("Line") = conv("Frag") _ $extract(conv("Line"), 2, *)
set conv("Frag") = indentString _ $replace($justify("", this("Indent") + multiLineCommand), $char(32), indentString)
set conv("Line") = conv("Frag") _ $extract(conv("Line"), 1+$length(indentString), *)
} else {
// Ignore indentation for ObjectScript line labels
// NOTE: Reference last language as "this" is the new line with no language.
Expand Down Expand Up @@ -335,6 +337,9 @@ ClassMethod InsertFragment(ByRef last, ByRef this)

ClassMethod ReadFragment(ByRef last, ByRef this, ByRef next)
{
set thisIsWhiteSpace = 0
set nextIsWhiteSpace = 0

set last("Lang") = this("Lang")
set last("Type") = this("Type")
set last("Frag") = this("Frag")
Expand All @@ -347,15 +352,37 @@ ClassMethod ReadFragment(ByRef last, ByRef this, ByRef next)
set this("Lang") = next("Lang")
set this("Type") = next("Type")
set this("Frag") = next("Frag")

// Prevent auto-indentation of embedded HTML
if (this("Type") = $$$HTMLWhiteSpace) && (this("Lang") = "HTML") {
set this("Type") = $$$HTMLText
}

if 'this("SyntaxStream").AtEnd {
set nextLine = this("SyntaxStream").ReadLine()
set next("Lang") = $piece(nextLine, ",", 1)
set next("Type") = $piece(nextLine, ",", 2)
set next("Frag") = $piece(nextLine, ",", 3, *)
// Get next code fragment, combining white space characters
set mixedIndent = 1
while mixedIndent {
set nextLine = this("SyntaxStream").ReadLine()
set next("Lang") = $piece(nextLine, ",", 1)
set next("Type") = $piece(nextLine, ",", 2)
set next("Frag") = $piece(nextLine, ",", 3, *)

// Checks if "next" is a different white space character than "this" and combines
set thisIsWhiteSpace = ($extract(this("Frag")) = $c(9)) || ($extract(this("Frag")) = $c(32))
set nextIsWhiteSpace = ($extract(next("Frag")) = $c(9)) || ($extract(next("Frag")) = $c(32))
set mixedIndent = (thisIsWhiteSpace) && (nextIsWhiteSpace)
if mixedIndent {
set this("Frag") = this("Frag") _ next("Frag")
}
}
} else {
set this("AtEnd") = 1
}

// Resume auto-indentation at close of embedded block
if (($extract(this("Frag")) = $c(32)) || ($extract(this("Frag")) = $c(9))) && (next("Lang") [ "COS") && (next("Frag") = ">") {
set this("Type") = $$$HTMLWhiteSpace
}

if ..#DEBUG, $data(nextLine) write nextLine, !
}
Expand All @@ -364,7 +391,7 @@ ClassMethod ParseFragment(ByRef last, ByRef this, ByRef next, ByRef conv)
{
// Indentation
if $case(this("Lang"),
"COS":$case(this("Type"), $$$COSWhiteSpace:1, $$$COSComment:1, :0),
"COS":$case(this("Type"), $$$COSWhiteSpace:1, :0),
"CSS":$case(this("Type"), $$$CSSWhiteSpace:1, $$$CSSCstyleComment:1, :0),
"HTML":$case(this("Type"), $$$HTMLWhiteSpace:1, $$$HTMLComment:1, :0),
"JAVASCRIPT":$case(this("Type"), $$$JAVASCRIPTWhiteSpace:1, $$$JAVASCRIPTComment:1, :0),
Expand Down Expand Up @@ -580,7 +607,6 @@ ClassMethod ParseFragmentCOS(ByRef last, ByRef this, ByRef next, ByRef conv)
// &javascript
if this("Type") = $$$COSJavascript {
set conv("Frag") = this("Frag")
if $zconvert(conv("Frag"), "L") = "js" set conv("Frag") = "javascript"
if ##class(pkg.isc.codetidy.Utils).GetUseCapitals() = 0 {
set conv("Frag") = $zconvert(conv("Frag"), "l")
} else {
Expand Down
2 changes: 1 addition & 1 deletion module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Export generator="IRIS" version="26">
<Document name="isc.codetidy.ZPM"><Module>
<Name>isc.codetidy</Name>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<Packaging>module</Packaging>
<Resource Name="pkg.isc.codetidy.PKG" Directory="cls" />
<Resource Name="pkg.isc.codetidy.CodeTidy.INC" Directory="inc" />
Expand Down
29 changes: 29 additions & 0 deletions tests/_reference/after/TestPackage.BlockCommentTestClass.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Class TestPackage.BlockCommentTestClass
{

Method BlockCommentTest()
{
/*
Do not change
my indentation
even if inconsistent
or containing "code"
set foo = "bar"
if (foo = "bar") {
do thing
}
*/

set foo = "bar"

/* Or even if
there are
multiple blocks,
multiple openings /*
or misaligned closings
within the block
*/
}

}

83 changes: 83 additions & 0 deletions tests/_reference/after/TestPackage.HTMLIndentTestClass.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Class TestPackage.HTMLIndentTestClass
{

Method HTMLGoodIndent()
{
&html<
<html>
<head></head>
<body></body>
>
}

Method HTMLBlockIndent()
{
&html<
<html>
<head></head>
<body></body>
>
}

Method HTMLBadIndent()
{
&html<
<html>
<head>
</head>
<body>
</body>
>
}

Method HTMLIfBlock()
{
if 1 {
&html<
<html>
<head></head>
<body></body>
>
}
}

Method HTMLUntidyIfBlock()
{
if 1 {
if 1 {
&html<
<html>
<head></head>
<body></body>
>
} else {
&html<
<html>
<head>
</head>
<body>
</body>
>
}
}
}

Method HTMLOneLine()
{
&html<<a href='url'>Link</a>>
}

Method HTMLMarker()
{
&htmlabc<
<html>
<body>
4 > 3
5>>7 >>>
4 > 2 >>
</body>
>cba
}

}

110 changes: 110 additions & 0 deletions tests/_reference/after/TestPackage.JSIndentTestClass.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
Class TestPackage.JSIndentTestClass
{

Method JavaScriptTidy()
{
&javascript<
function foo(bar)
{
// This is a comment
return true
}
>
}

Method JavaScriptUnindent()
{
&javascript<
function foo(bar)
{
// This is a comment
return true
}
>
}

Method JavaScriptNeedsIndent()
{
&javascript<
function foo(bar)
{
// This is a comment
return true
}
>
}

Method JavaScriptTwoBlocks()
{
&javascript<
function foo(bar)
{
// This is a comment
return true
}
>
&javascript<
function foo(bar)
{
// This is a comment
return true
}
>
}

Method JavaScriptOneLine()
{
&javascript<function foo(bar) {return true}>
}

Method JavaScriptVeryMessy()
{
if 1 {
&javascript<
function foo(bar)
{
return true
}


>
}
}

Method JavaScriptMarker()
{
&javascript123<
function foo(bar)
{
if (6 > 5) {
return True
}
}
>321
}

Method JavaScriptAbbrev()
{
if 1 {
&js<
function foo(bar)
{
return true
}


>
}

&jsabc<
function foo(bar)
{
if (6 > 5) {
return True
}
}
>cba
}

}

15 changes: 15 additions & 0 deletions tests/_reference/after/TestPackage.JSONBracketMatching.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class TestPackage.JSONBracketMatching
{

ClassMethod TestJSONArray() As %Status
{
set example = [{
"foo":"bar"
},
{
"bar":"foo"
}]
}

}

15 changes: 15 additions & 0 deletions tests/_reference/after/TestPackage.JSONBracketMixed.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class TestPackage.JSONBracketMixed
{

ClassMethod TestJSONArray() As %Status
{
set example = [{
"foo":"bar"
},
{
"bar":"foo"
}]
}

}

15 changes: 15 additions & 0 deletions tests/_reference/after/TestPackage.JSONBracketOneLine.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class TestPackage.JSONBracketOneLine
{

ClassMethod TestJSONArray() As %Status
{
set example = [{
"foo":"bar"
},
{
"bar":"foo"
}]
}

}

15 changes: 15 additions & 0 deletions tests/_reference/after/TestPackage.JSONIndent.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class TestPackage.JSONIndent
{

ClassMethod TestJSONArray() As %Status
{
set example = [{
"foo":"bar"
},
{
"bar":"foo"
}]
}

}

Loading

0 comments on commit 2f6c1ea

Please sign in to comment.