-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
preprocessor: add support for code directive
A code directive is a txtar-based directive, that contains a single file. It results in a ```-block, with the language of that block taken from the extension of the single file in the txtar archive. The basename of the file is irrelevant. The contents of the code directive are formatted, unless the #nofmt tag is used. Preprocessor-No-Write-Cache: true Signed-off-by: Paul Jolly <[email protected]> Change-Id: If68a9b0a10a2224e8841dfed73867fd41837b584 Dispatch-Trailer: {"type":"trybot","CL":1169683,"patchset":1,"ref":"refs/changes/83/1169683/1","targetBranch":"alpha"}
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 The CUE Authors | ||
// | ||
// Licensed 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. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
fnCode = "code" | ||
) | ||
|
||
// A codeNode is used to create a simple ```-based code block for a single file | ||
// whose contents are formatted. This ensures good formatting but also valid | ||
// code. | ||
type codeNode struct { | ||
txtarNode | ||
} | ||
|
||
func (c *codeNode) nodeType() string { | ||
return fnCode | ||
} | ||
|
||
var _ runnableNode = (*codeNode)(nil) | ||
|
||
type codeNodeRunContext struct { | ||
*txtarRunContext | ||
} | ||
|
||
func (s *codeNode) validate() error { | ||
if l := len(s.analysis.fileNames); l != 1 { | ||
return fmt.Errorf("code nodes can only contain one file in the txtar archive") | ||
} | ||
return nil | ||
} | ||
|
||
func (s *codeNode) run() runnable { | ||
return &codeNodeRunContext{ | ||
txtarRunContext: &txtarRunContext{ | ||
txtarNode: s.txtarNode, | ||
executionContext: s.executionContext, | ||
bufferedErrorContext: &errorContextBuffer{ | ||
executionContext: s.executionContext, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (s *codeNodeRunContext) run() (err error) { | ||
defer recoverFatalError(&err) | ||
|
||
if err := s.formatFiles(); err != nil { | ||
return errorIfInError(s) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *codeNode) writeTransformTo(b *bytes.Buffer) error { | ||
p := bufPrintf(b) | ||
|
||
// There will only be one file | ||
a := s.analysis.fileNames[0] | ||
f := s.effectiveArchive.Files[0] | ||
|
||
// TODO tidy up templating etc | ||
p("```%s\n", a.Language) | ||
p("%s", f.Data) | ||
p("```") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters