Skip to content

Commit

Permalink
more extraction fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
calldelegation committed Nov 20, 2023
1 parent 5a5cffe commit 70536df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/guides.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# note: must match the names in test.spec.ts
guide:
- "dev quickstart"
- "dev quickstart"
- "intro to sway"

steps:
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/docs/intro-to-sway/contract-functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Our first function enables sellers to list an item for sale. They can specify th

### Updating list storage

The initial step involves incrementing the `item_counter` in storage, which will serve as the item's ID. In Sway, the standard library provides `read()`, `write()`, and `try_read()` methods to access or manipulate contract storage. It's advisable to use `try_read()` when possible to prevent potential issues arising from accessing uninitialized storage. In this process, we read the current count of listed items, modify it, and then store the updated count back into storage.
The initial step involves incrementing the `item_counter` in storage, which will serve as the item's ID. In Sway, all storage variables are contained within the `storage` keyword, ensuring clarity and preventing conflicts with other variable names. This also allows developers to easily track when and where storage is accessed or altered. The standard library in Sway provides `read()`, `write()`, and `try_read()` methods to access or manipulate contract storage. It's advisable to use `try_read()` when possible to prevent potential issues arising from accessing uninitialized storage. In this case, we read the current count of listed items, modify it, and then store the updated count back into storage, making use of the well-organized and conflict-free storage system.

When a function returns an `Option` or `Result` type, we can use `unwrap()` to access its inner value. For instance, `try_read()` returns an `Option` type. If it yields `Some`, we get the contained value; but if it returns `None`, the contract call is immediately halted.

Expand Down
22 changes: 17 additions & 5 deletions src/lib/plugins/code-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function extractLines(
}
}

function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function extractCommentBlock(
content: string,
comment: string,
Expand All @@ -64,18 +68,26 @@ function extractCommentBlock(
let lineEnd = -1;
const anchorStack: string[] = [];

const endCommentType = getEndCommentType(commentType);
const endCommentType = getEndCommentType(commentType) || '';

const startAnchor = `${commentType} ANCHOR: ${comment}${endCommentType}`;
const endAnchor = `${commentType} ANCHOR_END: ${comment}${endCommentType}`;
const startAnchorRegex = new RegExp(
`${escapeRegExp(commentType)}\\s*ANCHOR\\s*:\\s*${escapeRegExp(
comment
)}\\s*${escapeRegExp(endCommentType)}`
);
const endAnchorRegex = new RegExp(
`${escapeRegExp(commentType)}\\s*ANCHOR_END\\s*:\\s*${escapeRegExp(
comment
)}\\s*${escapeRegExp(endCommentType)}`
);

for (let i = 0; i < lines.length; i++) {
if (lines[i].includes(startAnchor)) {
if (startAnchorRegex.test(lines[i])) {
if (lineStart === -1) {
lineStart = i;
}
anchorStack.push('anchor');
} else if (lines[i].includes(endAnchor)) {
} else if (endAnchorRegex.test(lines[i])) {
anchorStack.pop();
if (anchorStack.length === 0 && lineEnd === -1) {
lineEnd = i;
Expand Down

0 comments on commit 70536df

Please sign in to comment.