FormatTokenLexer;
117 if(LHS.
URL.empty() != RHS.
URL.empty())
118 returnLHS.
URL.empty() < RHS.
URL.empty();
119 if(
intRes = LHS.
URL.compare_insensitive(RHS.
URL))
136FileContents(
Env.getSourceManager().getBufferData(
Env.getFileID())) {
141std::pair<tooling::Replacements, unsigned>
151std::tie(References, FirstNonImportLine) =
152parseModuleReferences(Keywords, AnnotatedLines);
154 if(References.empty())
159InsertionPoint.
setEnd(References[References.size() - 1].Range.getEnd());
161References = sortModuleReferences(References);
163std::string ReferencesText;
164 for(
unsignedI = 0,
E= References.size(); I !=
E; ++I) {
166appendReference(ReferencesText,
Reference);
169ReferencesText +=
"\n";
173(
Reference.IsExport != References[I + 1].IsExport ||
174 Reference.Category != References[I + 1].Category)) {
175ReferencesText +=
"\n";
179StringRef PreviousText = getSourceText(InsertionPoint);
180 if(ReferencesText == PreviousText)
190 unsignedPreviousSize = PreviousText.size();
191 while(ReferencesText.size() < PreviousSize)
192ReferencesText +=
" ";
196!(FirstNonImportLine->
First->
is(tok::comment) &&
198ReferencesText +=
"\n";
201LLVM_DEBUG(llvm::dbgs() <<
"Replacing imports:\n" 202<< PreviousText <<
"\nwith:\n" 203<< ReferencesText <<
"\n");
210llvm::errs() <<
toString(std::move(Err)) <<
"\n";
223StringRef FileContents;
225 voidskipComments() { Current = skipComments(Current); }
227FormatToken *skipComments(FormatToken *Tok) {
228 while(Tok && Tok->is(tok::comment))
234Current = Current->Next;
236 if(!Current || Current == LineEnd->
Next) {
239Current = &invalidToken;
243StringRef getSourceText(SourceRange
Range) {
244 returngetSourceText(
Range.getBegin(),
Range.getEnd());
247StringRef getSourceText(SourceLocation
Begin, SourceLocation End) {
249 returnFileContents.substr(
SM.getFileOffset(
Begin),
250 SM.getFileOffset(End) -
SM.getFileOffset(
Begin));
257SmallVector<JsModuleReference, 16>
258sortModuleReferences(
constSmallVector<JsModuleReference, 16> &References) {
263 const auto*Start = References.begin();
264SmallVector<JsModuleReference, 16> ReferencesSorted;
265 while(Start != References.end()) {
266 while(Start != References.end() && Start->FormattingOff) {
268ReferencesSorted.push_back(*Start);
271SmallVector<JsModuleReference, 16> SortChunk;
272 while(Start != References.end() && !Start->FormattingOff) {
274SortChunk.push_back(*Start);
277stable_sort(SortChunk);
278mergeModuleReferences(SortChunk);
279ReferencesSorted.insert(ReferencesSorted.end(), SortChunk.begin(),
282 returnReferencesSorted;
294 voidmergeModuleReferences(SmallVector<JsModuleReference, 16> &References) {
295 if(References.empty())
297JsModuleReference *PreviousReference = References.begin();
298 auto*
Reference= std::next(References.begin());
307 Reference->IsExport != PreviousReference->IsExport ||
308 Reference->IsTypeOnly != PreviousReference->IsTypeOnly ||
309!PreviousReference->Prefix.empty() || !
Reference->Prefix.empty() ||
310!PreviousReference->DefaultImport.empty() ||
312PreviousReference->URL !=
Reference->URL) {
318PreviousReference->Symbols.append(
Reference->Symbols);
319PreviousReference->SymbolsMerged =
true;
326 voidappendReference(std::string &Buffer, JsModuleReference &
Reference) {
334SmallVector<JsImportedSymbol, 1> Symbols =
Reference.Symbols;
336[&](
constJsImportedSymbol &LHS,
constJsImportedSymbol &RHS) {
337 returnLHS.Symbol.compare_insensitive(RHS.Symbol) < 0;
341StringRef ReferenceStmt = getSourceText(
Reference.Range);
342Buffer += ReferenceStmt;
348 if(!Symbols.empty()) {
349Buffer += getSourceText(Symbols.front().Range);
350 for(
constJsImportedSymbol &Symbol : drop_begin(Symbols)) {
352Buffer += getSourceText(Symbol.Range);
362std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine *>
363parseModuleReferences(
constAdditionalKeywords &Keywords,
364SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
365SmallVector<JsModuleReference, 16> References;
366SourceLocation Start;
367AnnotatedLine *FirstNonImportLine =
nullptr;
368 boolAnyImportAffected =
false;
369 boolFormattingOff =
false;
370 for(
auto*
Line: AnnotatedLines) {
371assert(
Line->First);
372Current =
Line->First;
373LineEnd =
Line->Last;
376 while(Current && Current->is(tok::comment)) {
377StringRef CommentText = Current->TokenText.trim();
379FormattingOff =
true;
381FormattingOff =
false;
386 if(!References.empty()) {
387References.back().Range.setEnd(Current->Tok.getEndLoc());
388Start = Current->Tok.getEndLoc().getLocWithOffset(1);
392Current = Current->Next;
395 if(Start.isInvalid() || References.empty()) {
399Start =
Line->First->Tok.getLocation();
403FirstNonImportLine =
Line;
407 Reference.FormattingOff = FormattingOff;
411 if(!parseModuleReference(Keywords,
Reference)) {
412 if(!FirstNonImportLine)
413FirstNonImportLine =
Line;
416FirstNonImportLine =
nullptr;
417AnyImportAffected = AnyImportAffected ||
Line->Affected;
420llvm::dbgs() <<
"JsModuleReference: {" 421<<
"formatting_off: "<<
Reference.FormattingOff
422<<
", is_export: "<<
Reference.IsExport
426 for(
constJsImportedSymbol &Symbol :
Reference.Symbols)
427llvm::dbgs() <<
", "<< Symbol.Symbol <<
" as "<< Symbol.Alias;
428llvm::dbgs() <<
", text: "<< getSourceText(
Reference.Range);
429llvm::dbgs() <<
"}\n";
432Start = SourceLocation();
435 if(!AnyImportAffected)
437 returnstd::make_pair(References, FirstNonImportLine);
443 boolparseModuleReference(
constAdditionalKeywords &Keywords,
445 if(!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
447 Reference.IsExport = Current->is(tok::kw_export);
450 if(Current->isStringLiteral() && !
Reference.IsExport) {
454Current->TokenText.substr(1, Current->TokenText.size() - 2);
458 if(!parseModuleBindings(Keywords,
Reference))
461 if(Current->is(Keywords.kw_from)) {
464 if(!Current->isStringLiteral())
468Current->TokenText.substr(1, Current->TokenText.size() - 2);
469 if(
Reference.URL.starts_with(
"..")) {
472}
else if(
Reference.URL.starts_with(
".")) {
481 boolparseModuleBindings(
constAdditionalKeywords &Keywords,
483 if(parseStarBinding(Keywords,
Reference))
485 returnparseNamedBindings(Keywords,
Reference);
488 boolparseStarBinding(
constAdditionalKeywords &Keywords,
491 if(Current->is(Keywords.kw_type) && Current->Next &&
492Current->Next->is(tok::star)) {
496 if(Current->isNot(tok::star))
499 if(Current->isNot(Keywords.kw_as))
502 if(Current->isNot(tok::identifier))
509 boolparseNamedBindings(
constAdditionalKeywords &Keywords,
511 if(Current->is(Keywords.kw_type) && Current->Next &&
512Current->Next->isOneOf(tok::identifier, tok::l_brace)) {
518 if(!
Reference.IsExport && Current->is(tok::identifier)) {
519 Reference.DefaultImport = Current->TokenText;
521 if(Current->is(Keywords.kw_from))
524 if(Current->is(tok::equal)) {
527 while(Current->is(tok::identifier)) {
529 if(Current->is(tok::semi))
531 if(Current->isNot(tok::period))
536 if(Current->isNot(tok::comma))
540 if(Current->isNot(tok::l_brace))
544 Reference.SymbolsStart = Current->Tok.getEndLoc();
545 while(Current->isNot(tok::r_brace)) {
547 if(Current->is(tok::r_brace))
549 autoIsIdentifier = [](
const auto*Tok) {
550 returnTok->isOneOf(tok::identifier, tok::kw_default, tok::kw_template);
552 boolisTypeOnly = Current->is(Keywords.kw_type) && Current->Next &&
553IsIdentifier(Current->Next);
554 if(!isTypeOnly && !IsIdentifier(Current))
557JsImportedSymbol Symbol;
559Symbol.Range.setBegin(
560Current->getPreviousNonComment()->Next->WhitespaceRange.getBegin());
563Symbol.Symbol = Current->TokenText;
566 if(Current->is(Keywords.kw_as)) {
568 if(!IsIdentifier(Current))
570Symbol.Alias = Current->TokenText;
573Symbol.Range.setEnd(Current->Tok.getLocation());
576 if(!Current->isOneOf(tok::r_brace, tok::comma))
579 Reference.SymbolsEnd = Current->Tok.getLocation();
582 if(Current->Previous->is(tok::comma))
583 Reference.SymbolsEnd = Current->Previous->Tok.getLocation();
Defines the Diagnostic-related interfaces.
Various functions to configurably format source code.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
This file implements a sorter for JavaScript ES6 imports.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
This file declares an abstract TokenAnalyzer, and associated helper classes.
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
A trivial tuple used to represent a source range.
void setEnd(SourceLocation e)
SourceLocation getEndLoc() const
void startToken()
Reset all flags to cleared.
bool computeAffectedLines(SmallVectorImpl< AnnotatedLine * > &Lines)
SourceManager & getSourceManager() const
static std::unique_ptr< Environment > make(StringRef Code, StringRef FileName, ArrayRef< tooling::Range > Ranges, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
std::pair< tooling::Replacements, unsigned > analyze(TokenAnnotator &Annotator, SmallVectorImpl< AnnotatedLine * > &AnnotatedLines, FormatTokenLexer &Tokens) override
JavaScriptImportSorter(const Environment &Env, const FormatStyle &Style)
AffectedRangeManager AffectedRangeMgr
std::pair< tooling::Replacements, unsigned > process(bool SkipAnnotation=false)
Determines extra information about the tokens comprising an UnwrappedLine.
bool operator<(const JsModuleReference &LHS, const JsModuleReference &RHS)
bool isClangFormatOff(StringRef Comment)
tooling::Replacements sortJavaScriptImports(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName)
bool isClangFormatOn(StringRef Comment)
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
The FormatStyle is used to configure the formatting to follow specific guidelines.
A wrapper around a Token storing information about the whitespace characters preceding it.
StringRef TokenText
The raw text of the token.
FormatToken * Next
The next token in the unwrapped line.
unsigned NewlinesBefore
The number of newlines immediately before the Token.
bool is(tok::TokenKind Kind) const
bool operator==(const JsImportedSymbol &RHS) const
SmallVector< JsImportedSymbol, 1 > Symbols
SourceLocation SymbolsStart
ReferenceCategory Category
SourceLocation SymbolsEnd
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4