Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix more tests, get rid of any flowtype
Browse files Browse the repository at this point in the history
  • Loading branch information
lloiser committed Jan 15, 2019
1 parent a9ad6ac commit 0b21c70
Show file tree
Hide file tree
Showing 33 changed files with 163 additions and 175 deletions.
7 changes: 5 additions & 2 deletions flow-libs/atom.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ declare class atom$Decoration {
}

declare class atom$DisplayMarkerLayer {
id: number,
id: string,
destroy(): void,
clear(): void,
isDestroyed(): boolean,
Expand Down Expand Up @@ -663,6 +663,7 @@ declare class atom$Range {
end: atom$Point,
isEmpty(): boolean,
isEqual(otherRange: atom$RangeLike): boolean,
isSingleLine(): boolean,
intersectsWith(otherRange: atom$RangeLike, exclusive?: boolean): boolean,
containsPoint(point: atom$PointLike, exclusive?: boolean): boolean,
containsRange(other: atom$Range, exclusive?: boolean): boolean,
Expand All @@ -686,6 +687,7 @@ type atom$StatusBarTile = {

declare class atom$ScopeDescriptor {
constructor(object: {scopes: Array<string>}): void,
scopes: Array<string>,
getScopesArray(): Array<string>,
}

Expand Down Expand Up @@ -957,7 +959,7 @@ declare class atom$TextEditor extends atom$Model {
// Markers
addMarkerLayer(): atom$DisplayMarkerLayer,
getDefaultMarkerLayer(): atom$DisplayMarkerLayer,
getMarkerLayer(id: number): atom$DisplayMarkerLayer,
getMarkerLayer(id: string): atom$DisplayMarkerLayer,
markBufferPosition(position: atom$PointLike, options?: MarkerOptions): atom$Marker,
markBufferRange(range: atom$RangeLike, options?: MarkerOptions): atom$Marker,
markScreenRange(range: atom$RangeLike, options?: MarkerOptions): atom$Marker,
Expand Down Expand Up @@ -1768,6 +1770,7 @@ declare class atom$Project {
}

type TextBufferScanIterator = (arg: {
row: number,
match: Array<string>,
matchText: string,
range: atom$Range,
Expand Down
9 changes: 6 additions & 3 deletions lib/autocomplete/gocodeprovider-helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @flow

import path from 'path'
import type { AtomPoint } from './provider'

const vendorString = '/vendor/'

export function wantedPackage(buffer: any, pos: AtomPoint) {
export function wantedPackage(buffer: atom$TextBuffer, pos: atom$Point) {
// get the pkg the user tries to autocomplete from the current line
const lineTillPos = buffer.getTextInRange([[pos.row, 0], pos])
const matches = lineTillPos.match(/(\w+)\.$/)
Expand All @@ -15,7 +14,11 @@ export function wantedPackage(buffer: any, pos: AtomPoint) {
return matches[1]
}

export function addImport(buffer: any, pkg: string, offset: number) {
export function addImport(
buffer: atom$TextBuffer,
pkg: string,
offset: number
) {
// find the "package ..." statement
let row = -1
buffer.scan(/^package /, result => {
Expand Down
15 changes: 8 additions & 7 deletions lib/autocomplete/gocodeprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class GocodeProvider implements AutocompleteProvider {

characterIsSuppressed(
char: string,
scopeDescriptor: { scopes: Array<string> }
scopeDescriptor: atom$ScopeDescriptor
): boolean {
if (
scopeDescriptor &&
Expand Down Expand Up @@ -188,7 +188,9 @@ class GocodeProvider implements AutocompleteProvider {
}
}

const sameFile = this.currentFile === editor.getPath()
const file = editor.getPath() || ''

const sameFile = this.currentFile === file
const sameLine =
this.currentRow >= 0 && this.currentRow === bufferPosition.row
const movingForward =
Expand Down Expand Up @@ -216,7 +218,7 @@ class GocodeProvider implements AutocompleteProvider {
}).map(s =>
Object.assign({}, s, { replacementPrefix: newPrefix })
)
this.currentFile = editor.getPath()
this.currentFile = file
this.currentRow = bufferPosition.row
this.currentColumn = bufferPosition.column
resolve(fil)
Expand Down Expand Up @@ -262,7 +264,6 @@ class GocodeProvider implements AutocompleteProvider {
resolve([])
return
}
const file = buffer.getPath()
const args = ['-f=json', 'autocomplete', file, offset.toString()]
if (this.proposeBuiltins) args.unshift('-builtin')
if (this.unimportedPackages) args.unshift('-unimported-packages')
Expand Down Expand Up @@ -313,7 +314,7 @@ class GocodeProvider implements AutocompleteProvider {
}

this.currentSuggestions = suggestions
this.currentFile = editor.getPath()
this.currentFile = file
this.currentRow = bufferPosition.row
this.currentColumn = bufferPosition.column
const newPrefix = editor.getTextInBufferRange([
Expand Down Expand Up @@ -386,8 +387,8 @@ class GocodeProvider implements AutocompleteProvider {

mapMessages(
res: RawGoCodeSuggestion,
editor: any,
position: any
editor: TextEditor,
position: atom$Point
): Array<Suggestion> {
const candidates: GoCodeSuggestion[] = res[1]
if (!candidates || !candidates.length) {
Expand Down
14 changes: 4 additions & 10 deletions lib/autocomplete/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@ export type Suggestion = {
descriptionMoreURL?: string
}

export type AtomPoint = {
row: number,
column: number,
copy: () => AtomPoint
}

export type SuggestionRequest = {
editor: any,
bufferPosition: AtomPoint,
editor: TextEditor,
bufferPosition: atom$Point,
scopeDescriptor: string,
prefix: string,
activatedManually: boolean
Expand All @@ -59,8 +53,8 @@ export interface AutocompleteProvider {

+dipose?: () => void;
+onDidInsertSuggestion?: ({
editor: any,
triggerPosition: any,
editor: TextEditor,
triggerPosition: atom$Point,
suggestion: Suggestion
}) => void;

Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Bootstrap {

subscribeToEvents() {
const activationHook = (hookName, fn) => {
const hooks: any = (atom.packages: any).triggeredActivationHooks
const hooks = atom.packages.triggeredActivationHooks
if (hooks && hooks.has(hookName)) {
fn()
return
Expand Down
6 changes: 3 additions & 3 deletions lib/build/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Builder {
goconfig: GoConfig,
linter: () => LinterDelegate,
output: OutputManager,
busySignal: () => any
busySignal: () => ?BusySignalService
) {
this.goconfig = goconfig
this.linter = linter
Expand Down Expand Up @@ -64,15 +64,15 @@ class Builder {
}
}

async build(editor: any, path: string = editor.getPath()): Promise<any> {
async build(editor: TextEditor, path: string): Promise<void> {
if (!atom.config.get('go-plus.config.compileOnSave')) {
return
}
if (!isValidEditor(editor)) {
throw new Error('invalid editor')
}
this.deleteMessages()
const options = this.goconfig.executor.getOptions('file')
const options = this.goconfig.executor.getOptions('file', editor)
const cmd = await this.goconfig.locator.findTool('go')
if (!cmd) {
throw new Error('cannot find go tool')
Expand Down
8 changes: 4 additions & 4 deletions lib/config/executor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @flow

import { BufferedProcess } from 'atom'
import { BufferedProcess, TextEditor } from 'atom'
import { spawnSync } from 'child_process'
import { getenvironment } from './environment'
import fs from 'fs-extra'
import path from 'path'
import { getEditor, projectPath } from '../utils'
import { projectPath } from '../utils'

export type ExecutorOptions = {
timeout?: number,
Expand Down Expand Up @@ -196,7 +196,7 @@ class Executor {

getOptions(
kind: 'file' | 'project' = 'file',
editor: any = getEditor()
editor?: ?TextEditor
): ExecutorOptions {
const result: ExecutorOptions = this.getDefaultOptions(kind, editor)
return this.ensureOptions(result)
Expand Down Expand Up @@ -225,7 +225,7 @@ class Executor {

getDefaultOptions(
key: 'file' | 'project' = 'file',
editor: any = getEditor()
editor?: ?TextEditor
): ExecutorOptions {
let options = {}
switch (key) {
Expand Down
13 changes: 8 additions & 5 deletions lib/config/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { Locator } from './locator'
import type { ExecutorOptions, ExecResult } from './executor'
import type { FindResult, Runtime } from './locator'

interface PublicExecutor {
exec(string, Array<string>, ExecutorOptions): Promise<ExecResult>;
execSync(string, Array<string>, ExecutorOptions): ExecResult;
getOptions('file', editor: TextEditor): ExecutorOptions;
getOptions('project', editor?: ?TextEditor): ExecutorOptions;
}

export type GoConfig = {
executor: {
exec: (string, Array<string>, ExecutorOptions) => Promise<ExecResult>,
execSync: (string, Array<string>, ExecutorOptions) => ExecResult,
getOptions: ('file' | 'project', any) => ExecutorOptions
},
executor: PublicExecutor,
locator: {
runtimes: () => Promise<Array<Runtime>>,
runtime: () => Promise<false | Runtime>,
Expand Down
14 changes: 1 addition & 13 deletions lib/doc/godoc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import { CompositeDisposable, Point, Range } from 'atom'
import { CompositeDisposable, Range } from 'atom'
import { GodocPanel } from './godoc-panel'
import { utf8OffsetForBufferPosition } from '../utils'
import { buildGuruArchive } from '../guru-utils'
Expand Down Expand Up @@ -188,18 +188,6 @@ ${doc.doc}`
})
}

editorByteOffset(editor: any): number {
const cursor = editor.getLastCursor()
const range = cursor.getCurrentWordBufferRange()
const middle = new Point(
range.start.row,
Math.floor((range.start.column + range.end.column) / 2)
)
const charOffset = editor.buffer.characterIndexForPosition(middle)
const text = editor.getText().substring(0, charOffset)
return Buffer.byteLength(text, 'utf8')
}

getPanel() {
return this.panelModel
}
Expand Down
2 changes: 1 addition & 1 deletion lib/format/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Formatter {
console.log('skipping format, could not find tool', tool) // eslint-disable-line no-console
return null
}
const options = this.goconfig.executor.getOptions('project')
const options = this.goconfig.executor.getOptions('project', editor)
options.input = editor.getText()
const args = []
if (tool === 'goimports') {
Expand Down
4 changes: 2 additions & 2 deletions lib/get/get-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type GetResult = {
args: Array<string>
}

type MultiGetResult = {
export type MultiGetResult = {
success: boolean,
result: ?GetResult,
results: ?Array<GetResult>
Expand Down Expand Up @@ -225,7 +225,7 @@ class GetManager {
return { success: false, result: null, results: null }
}

const executorOptions = this.goconfig.executor.getOptions('file')
const executorOptions = this.goconfig.executor.getOptions('project')
const timeout = atom.config.get('go-plus.get.timeout')
if (typeof timeout === 'number') {
executorOptions.timeout = timeout
Expand Down
3 changes: 2 additions & 1 deletion lib/get/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import { Disposable } from 'atom'
import { GetManager } from './get-manager'
import type { MultiGetResult } from './get-manager'
import type { GoConfig } from './../config/service'

import type { InteractiveGetOptions } from './get-manager'

export type GoGet = {
get: InteractiveGetOptions => Promise<any>,
get: InteractiveGetOptions => Promise<?MultiGetResult>,
register: (string, ?Function) => Disposable
}

Expand Down
28 changes: 16 additions & 12 deletions lib/guru-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @flow

import type { TextEditor } from 'atom'

import {
getEditor,
getCursorPosition,
isValidEditor,
utf8OffsetForBufferPosition
Expand All @@ -17,7 +18,7 @@ const scopedModes = [
'referrers'
]

function buildGuruArchive(editor: any = null) {
function buildGuruArchive(editor: ?TextEditor) {
let archive = ''
const editors = editor ? [editor] : atom.workspace.getTextEditors()
for (const e of editors) {
Expand All @@ -33,14 +34,18 @@ function buildGuruArchive(editor: any = null) {
function computeArgs(
mode: string,
options: ?{ gopath: string },
editor: any = getEditor(),
editor: TextEditor,
pos: number = currentCursorOffset(editor)
): ?Array<string> {
if (!mode || !editor || (!pos && pos !== 0)) {
return undefined
}

const filePath = editor.getPath()
if (!filePath) {
return undefined
}

const args = ['-json']
if (scopedModes.includes(mode)) {
const src = 'src/'
Expand All @@ -59,15 +64,16 @@ function computeArgs(
return args
}

function currentCursorOffset(editor: any = getEditor()): number {
const pos = adjustPositionForGuru(getCursorPosition(), editor)
function currentCursorOffset(editor: TextEditor): number {
let pos = getCursorPosition(editor)
if (!pos) {
return 0
}
pos = adjustPositionForGuru(pos, editor)
return utf8OffsetForBufferPosition(pos, editor)
}

function adjustPositionForGuru(
pos: any = getCursorPosition(),
editor: any = getEditor()
) {
function adjustPositionForGuru(pos: atom$Point, editor: TextEditor) {
if (!pos) {
return pos
}
Expand All @@ -78,9 +84,7 @@ function adjustPositionForGuru(
// at the given position is considered a part of an identifier.
// If not step back 1 char as it might contain a valid identifier.
const char = editor.getTextInBufferRange([pos, pos.translate([0, 1])])
const nonWordChars = editor.getNonWordCharacters(
editor.scopeDescriptorForBufferPosition(pos)
)
const nonWordChars = editor.getNonWordCharacters(pos)
if (nonWordChars.indexOf(char) >= 0 || /\s/.test(char)) {
return pos.translate([0, -1])
}
Expand Down

0 comments on commit 0b21c70

Please sign in to comment.