Skip to content

Commit

Permalink
PR feedback: remove more instanceof, rename splitStringIntoLines to s…
Browse files Browse the repository at this point in the history
…plitIntoLines, remove TokenKind limitations for onReferences
  • Loading branch information
triwav committed Nov 10, 2020
1 parent 09b80d3 commit f5a3f13
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
6 changes: 3 additions & 3 deletions scripts/compile-doc-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DocCompiler {

public async processDoc(docPath: string) {
let contents = fsExtra.readFileSync(docPath).toString();
this.lines = util.splitStringIntoLines(contents);
this.lines = util.splitIntoLines(contents);
this.index = 0;
while (this.index < this.lines.length) {
let line = this.lines[this.index];
Expand Down Expand Up @@ -61,7 +61,7 @@ class DocCompiler {

var transpiledStartIndex: number;
var transpiledStopIndex: number;
//find the transpiled code block (there must be one after every
//find the transpiled code block (there must be one after every
//+2 to step past the last line of code, and the final ```
outer: for (var i = sourceStopIndex + 2; i < this.lines.length; i++) {
let line = this.lines[i];
Expand Down Expand Up @@ -122,4 +122,4 @@ new DocCompiler().run()
console.log.bind(console)
).catch(
console.error.bind(console)
);
);
2 changes: 1 addition & 1 deletion src/LanguageServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ describe('LanguageServer', () => {
}
});

it('should return an empty response if we entered on anything other than an identifier token', async () => {
it('should return an empty response if we entered on a token that should not return any results', async () => {
let references = await svr.onReferences({
textDocument: {
uri: functionDocument.uri
Expand Down
3 changes: 2 additions & 1 deletion src/LanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { BrsFile } from './files/BrsFile';
import type { Token } from './lexer';
import { Lexer } from './lexer';
import { DiagnosticCollection } from './DiagnosticCollection';
import { isBrsFile } from './astUtils/reflection';

export class LanguageServer {
//cast undefined as any to get around strictNullChecks...it's ok in this case
Expand Down Expand Up @@ -962,7 +963,7 @@ export class LanguageServer {
const pathAbsolute = util.uriToPath(params.textDocument.uri);
for (const workspace of this.getWorkspaces()) {
const file = workspace.builder.program.getFileByPathAbsolute(pathAbsolute);
if (file instanceof BrsFile) {
if (isBrsFile(file)) {
const symbols = await file.getDocumentSymbols();
return symbols;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ export class Program {
const results = await Promise.all(
Object.keys(this.files).map(async key => {
const file = this.files[key];
if (file instanceof BrsFile) {
if (isBrsFile(file)) {
return file.getWorkspaceSymbols();
}
return [];
Expand All @@ -683,7 +683,7 @@ export class Program {
return [];
}

if (file instanceof BrsFile) {
if (isBrsFile(file)) {
return file.getDefinition(position);
} else {
let results = [] as Location[];
Expand Down Expand Up @@ -719,7 +719,7 @@ export class Program {
const scopes = this.getScopesForFile(file);
for (const scope of scopes) {
for (const file of scope.getFiles()) {
if (file instanceof XmlFile) {
if (isXmlFile(file)) {
continue;
}

Expand Down
16 changes: 6 additions & 10 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { TranspileState } from '../parser/TranspileState';
import { Preprocessor } from '../preprocessor/Preprocessor';
import { LogLevel } from '../Logger';
import { serializeError } from 'serialize-error';
import { isCallExpression, isClassMethodStatement, isClassStatement, isCommentStatement, isDottedGetExpression, isFunctionExpression, isFunctionStatement, isFunctionType, isImportStatement, isLibraryStatement, isLiteralExpression, isNamespaceStatement, isStringType, isVariableExpression } from '../astUtils/reflection';
import { isCallExpression, isClassMethodStatement, isClassStatement, isCommentStatement, isDottedGetExpression, isFunctionExpression, isFunctionStatement, isFunctionType, isImportStatement, isLibraryStatement, isLiteralExpression, isNamespaceStatement, isStringType, isVariableExpression, isXmlFile } from '../astUtils/reflection';
import { createVisitor, WalkMode } from '../astUtils/visitors';
import { XmlFile } from './XmlFile';
import type { DependencyGraph } from '../DependencyGraph';
Expand Down Expand Up @@ -1137,6 +1137,7 @@ export class BrsFile {
//get the token at the position
const token = this.getTokenAt(position);

// While certain other tokens are allowed as local variables (AllowedLocalIdentifiers: https://github.com/rokucommunity/brighterscript/blob/master/src/lexer/TokenKind.ts#L418), these are converted by the parser to TokenKind.Identifier by the time we retrieve the token using getTokenAt
let definitionTokenTypes = [
TokenKind.Identifier,
TokenKind.StringLiteral
Expand Down Expand Up @@ -1177,7 +1178,7 @@ export class BrsFile {
//look through all files in scope for matches
for (const scope of this.program.getScopesForFile(this)) {
for (const file of scope.getFiles()) {
if (file instanceof XmlFile || filesSearched[file.pathAbsolute]) {
if (isXmlFile(file) || filesSearched[file.pathAbsolute]) {
continue;
}
filesSearched[file.pathAbsolute] = true;
Expand Down Expand Up @@ -1298,7 +1299,7 @@ export class BrsFile {
}
const documentation = functionComments.join('').trim();

const lines = util.splitStringIntoLines(this.fileContents);
const lines = util.splitIntoLines(this.fileContents);

const params = [] as ParameterInformation[];
for (const param of func.parameters) {
Expand All @@ -1317,18 +1318,13 @@ export class BrsFile {

let locations = [] as Location[];

// No need to actually look if they didn't select a token we can search against
if (callSiteToken?.kind !== TokenKind.Identifier) {
return locations;
}

const searchFor = callSiteToken.text.toLowerCase();

const scopes = this.program.getScopesForFile(this);

for (const scope of scopes) {
for (const file of scope.getFiles()) {
if (file instanceof XmlFile) {
if (isXmlFile(file)) {
continue;
}

Expand Down Expand Up @@ -1361,7 +1357,7 @@ export class BrsFile {
} else {
//create a source map from the original source code
let chunks = [] as (SourceNode | string)[];
let lines = util.splitStringIntoLines(this.fileContents);
let lines = util.splitIntoLines(this.fileContents);
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
let line = lines[lineIndex];
chunks.push(
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ export class Util {
}
}

public splitStringIntoLines(string: string) {
public splitIntoLines(string: string) {
return string.split(/\r?\n/g);
}

Expand All @@ -928,7 +928,7 @@ export class Util {
if (Array.isArray(string)) {
lines = string;
} else {
lines = this.splitStringIntoLines(string);
lines = this.splitIntoLines(string);
}

const start = range.start;
Expand Down

0 comments on commit f5a3f13

Please sign in to comment.