D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
SyntaxSuggest
/
CleanDocument
/
Filename :
cdesc-CleanDocument.ri
back
Copy
U:RDoc::NormalClass[iI"CleanDocument:ETI"!SyntaxSuggest::CleanDocument;TI"Object;To:RDoc::Markup::Document:@parts[o;;[1o:RDoc::Markup::Paragraph;[I"@Parses and sanitizes source into a lexically aware document;To:RDoc::Markup::BlankLine o; ;[I"BInternally the document is represented by an array with each ;TI"Lindex containing a CodeLine correlating to a line from the source code.;T@o; ;[I"2There are three main phases in the algorithm:;T@o:RDoc::Markup::List: @type:NUMBER:@items[o:RDoc::Markup::ListItem:@label0;[o; ;[I"!Sanitize/format input source;To;;0;[o; ;[I"Search for invalid blocks;To;;0;[o; ;[I"4Format invalid blocks into something meaningful;T@o; ;[I"'This class handles the first part.;T@o; ;[I"<The reason this class exists is to format input source ;TI"+for better/easier/cleaner exploration.;T@o; ;[ I"8The CodeSearch class operates at the line level so ;TI"9we must be careful to not introduce lines that look ;TI"Fvalid by themselves, but when removed will trigger syntax errors ;TI"or strange behavior.;T@o; ;[I"## Join Trailing slashes;T@o; ;[I"FCode with a trailing slash is logically treated as a single line:;T@o:RDoc::Markup::Verbatim;[I" 1 it "code can be split" \ ;TI"%2 "across multiple lines" do ;T:@format0o; ;[I"JIn this case removing line 2 would add a syntax error. We get around ;TI"Ithis by internally joining the two lines into a single "line" object;T@o; ;[I"### Logically Consecutive lines;T@o; ;[I"+Code that can be broken over multiple ;TI"7lines such as method calls are on different lines:;T@o;;[I" 1 User. ;TI""2 where(name: "schneems"). ;TI"3 first ;T;0o; ;[I"JRemoving line 2 can introduce a syntax error. To fix this, all lines ;TI"are joined into one.;T@o; ;[I"## Heredocs;T@o; ;[ I"NA heredoc is an way of defining a multi-line string. They can cause many ;TI"Tproblems. If left as a single line, the parser would try to parse the contents ;TI"Oas ruby code rather than as a string. Even without this problem, we still ;TI"#hit an issue with indentation:;T@o;;[ I"1 foo = <<~HEREDOC ;TI"82 "Be yourself; everyone else is already taken."" ;TI"3 ― Oscar Wilde ;TI"C4 puts "I look like ruby code" # but i'm still a heredoc ;TI"5 HEREDOC ;T;0o; ;[I"NIf we didn't join these lines then our algorithm would think that line 4 ;TI"Pis separate from the rest, has a higher indentation, then look at it first ;TI"and remove it.;T@o; ;[I"PIf the code evaluates line 5 by itself it will think line 5 is a constant, ;TI".remove it, and introduce a syntax errror.;T@o; ;[I"PAll of these problems are fixed by joining the whole heredoc into a single ;TI" line.;T@o; ;[I"## Comments and whitespace;T@o; ;[I"EComments can throw off the way the lexer tells us that the line ;TI"Blogically belongs with the next line. This is valid ruby but ;TI"3results in a different lex output than before:;T@o;;[ I" 1 User. ;TI""2 where(name: "schneems"). ;TI"3 # Comment here ;TI"4 first ;T;0o; ;[ I"BTo handle this we can replace comment lines with empty lines ;TI"Fand then re-lex the source. This removal and re-lexing preserves ;TI"Hline index and document size, but generates an easier to work with ;TI"document.;T: @fileI")lib/syntax_suggest/clean_document.rb;T:0@omit_headings_from_table_of_contents_below0;0;0[ [ [ [[I" class;T[[:public[[I"new;TI")lib/syntax_suggest/clean_document.rb;T[:protected[ [:private[ [I" instance;T[[;[[I" call;T@�[I"clean_sweep;T@�[I"join_consecutive!;T@�[I"join_groups;T@�[I"join_heredoc!;T@�[I"join_trailing_slash!;T@�[I" lines;T@�[I"take_while_including;T@�[I" to_s;T@�[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@�I"SyntaxSuggest;TcRDoc::NormalModule