Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff5d220

Browse files
committed
Add a better initial source
1 parent 96daa41 commit ff5d220

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Suspense, useState } from "react";
22
import { createRoot } from "react-dom/client";
33

4+
import initialSource from "./initialSource";
45
import Editor from "./Editor";
56
const Tree = React.lazy(() => import("./Tree"));
67

@@ -13,7 +14,7 @@ const TreeFallback: React.FC<TreeFallbackProps> = ({ cols }) => (
1314
);
1415

1516
const App: React.FC = () => {
16-
const [source, setSource] = useState<string>("1 + 2");
17+
const [source, setSource] = useState<string>(initialSource);
1718
const cols = 80;
1819

1920
return (

src/initialSource.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const initialSource = `# frozen_string_literal: true
2+
3+
require "json"
4+
require "pp"
5+
require "prettyprint"
6+
require "ripper"
7+
require "stringio"
8+
9+
require_relative "syntax_tree/formatter"
10+
require_relative "syntax_tree/node"
11+
require_relative "syntax_tree/parser"
12+
require_relative "syntax_tree/prettyprint"
13+
require_relative "syntax_tree/version"
14+
require_relative "syntax_tree/visitor"
15+
require_relative "syntax_tree/visitor/json_visitor"
16+
require_relative "syntax_tree/visitor/pretty_print_visitor"
17+
18+
module SyntaxTree
19+
# This holds references to objects that respond to both #parse and #format
20+
# so that we can use them in the CLI.
21+
HANDLERS = {}
22+
HANDLERS.default = SyntaxTree
23+
24+
# This is a hook provided so that plugins can register themselves as the
25+
# handler for a particular file type.
26+
def self.register_handler(extension, handler)
27+
HANDLERS[extension] = handler
28+
end
29+
30+
# Parses the given source and returns the syntax tree.
31+
def self.parse(source)
32+
parser = Parser.new(source)
33+
response = parser.parse
34+
response unless parser.error?
35+
end
36+
37+
# Parses the given source and returns the formatted source.
38+
def self.format(source)
39+
formatter = Formatter.new(source, [])
40+
parse(source).format(formatter)
41+
42+
formatter.flush
43+
formatter.output.join
44+
end
45+
46+
# Returns the source from the given filepath taking into account any potential
47+
# magic encoding comments.
48+
def self.read(filepath)
49+
encoding =
50+
File.open(filepath, "r") do |file|
51+
header = file.readline
52+
header += file.readline if header.start_with?("#!")
53+
Ripper.new(header).tap(&:parse).encoding
54+
end
55+
56+
File.read(filepath, encoding: encoding)
57+
end
58+
end
59+
`;
60+
61+
export default initialSource;

0 commit comments

Comments
 (0)