Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
- Add a Core.ts file. This is just for interfaces for the Core (F#)
  types. Move everything of that sort here.
- Remove `rewrapSelection` function that converted vscode Selection
  objects to Fable ones, because Fable can now work with the former.
- Other small formatting changes
  • Loading branch information
stkb committed Mar 20, 2021
1 parent ffa3ed2 commit 2ee936f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 68 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"paragraph",
"multi-root ready"
],
"version": "1.14.1-alpha.1",
"version": "1.14.1-alpha.2",
"publisher": "stkb",
"icon": "docs/images/logo.png",
"repository": {
Expand Down
10 changes: 4 additions & 6 deletions vscode/AutoWrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getWrappingColumn, maybeAutoWrap} from './Core'
import {Memento, ThemeColor, workspace, window, TextDocumentChangeEvent, TextEditor} from 'vscode'
import {Rewrap, applyEdit, catchErr, docLine, docType} from './Common'
const Rewrap: Rewrap = require('./core/Main')
import {applyEdit, catchErr, docLine, docType} from './Common'
import {EditorSettings, getCoreSettings, getEditorSettings} from './Settings'

/** Handler that's called if the text in the active editor changes */
Expand All @@ -27,12 +27,10 @@ const checkChange = (e: TextDocumentChangeEvent) => {

try {
const file = docType(doc)
const settings =
getCoreSettings(editor, cs => Rewrap.getWrappingColumn(file.path, cs))
const settings = getCoreSettings(editor, cs => getWrappingColumn(file.path, cs))
// maybeAutoWrap does more checks: that newText isn't empty, but is only
// whitespace. Don't call this in a promise: it causes timing issues.
const edit =
Rewrap.maybeAutoWrap(file, settings, newText, range.start, docLine(doc))
const edit = maybeAutoWrap(file, settings, newText, range.start, docLine(doc))
return applyEdit(editor, edit).then(null, catchErr)
}
catch(err) { catchErr(err) }
Expand Down
33 changes: 7 additions & 26 deletions vscode/Common.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import * as vscode from 'vscode'
import {Range, window} from 'vscode'
import {CoreSettings} from './Settings'
import {DocType} from './Core'
import {Range, Selection, TextDocument, window} from 'vscode'
import fixSelections from './FixSelections'
import GetCustomMarkers from './CustomLanguage'
const getCustomMarkers = GetCustomMarkers()

export interface DocType {
path: string,
language: string,
getMarkers: () => any,
}

export interface Rewrap {
getWrappingColumn(path: string, columns: number[])
maybeAutoWrap(docType: DocType, settings: CoreSettings, newText: string, pos: vscode.Position, docLine: any)
maybeChangeWrappingColumn(docState: any, columns: [number])
rewrap(docType: DocType, settings: CoreSettings, selections: any, docLine: any)
saveDocState(docState: any): void
}

/** Converts a selection-like object to a vscode Selection object */
const vscodeSelection = s =>
new vscode.Selection
(s.anchor.line, s.anchor.character, s.active.line, s.active.character)
new Selection(s.anchor.line, s.anchor.character, s.active.line, s.active.character)

/** Applies an edit to the document. Also fixes the selections afterwards. If
* the edit is empty this is a no-op */
Expand Down Expand Up @@ -56,8 +41,7 @@ export function applyEdit (editor, edit) {
if(!didEdit) return
if(wholeDocSelected) {
const wholeRange = getDocRange()
editor.selection =
new vscode.Selection(wholeRange.start, wholeRange.end)
editor.selection = new Selection(wholeRange.start, wholeRange.end)
}
else editor.selections = fixSelections(oldLines, selections, edit)
})
Expand Down Expand Up @@ -86,10 +70,7 @@ export function docLine(document) {

/** Gets the path and language of the document. These are used to determine the
* parser used for it. */
export function docType(document): DocType {
return {
path: document.fileName,
language: document.languageId,
getMarkers: () => getCustomMarkers(document.languageId),
}
export function docType(document: TextDocument): DocType {
const path = document.fileName, language = document.languageId
return {path, language, getMarkers: () => getCustomMarkers(language)}
}
56 changes: 56 additions & 0 deletions vscode/Core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const Types = require('./core/Types')
const Main = require('./core/Main')


export interface CustomMarkers { line: string, block: [string, string] }
export interface CustomMarkersStatic {
new(line: string, block: string[]): CustomMarkers
}

export interface DocState { filePath: string, version: number, selections: Selection[] }

export interface DocType {
path: string
language: string
getMarkers: () => CustomMarkers
}

export interface Edit {
startLine: number
endLine: number
lines: string[]
selections: Selection[]
}

export interface Position { line: number, character: number }

export interface Selection { anchor: Position, active: Position }

export interface Settings {
column: number
tabWidth: number
doubleSentenceSpacing: boolean
reformat: boolean
wholeComment: boolean
}

export const CustomMarkers: CustomMarkersStatic = Types.CustomMarkers

/** Gets the current wrapping column used for the given document */
export const getWrappingColumn: (path: string, columns: number[]) => number =
Main.getWrappingColumn

export const maybeAutoWrap:
(docType: DocType, settings: Settings,
newText: string, pos: Position, docLine: (i:number) => string) => Edit =
Main.maybeAutoWrap

export const maybeChangeWrappingColumn:
(docState: DocState, columns: number[]) => number = Main.maybeChangeWrappingColumn

export const rewrap:
(docType: DocType, settings: Settings,
selections: Selection[], docLine: (i:number) => string) => Edit =
Main.rewrap

export const saveDocState: (docState: DocState) => void = Main.saveDocState
36 changes: 10 additions & 26 deletions vscode/Extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const {DocState, Position, Selection} = require('./core/Types')
import {Rewrap, applyEdit, catchErr, docType, docLine} from './Common'
const Rewrap: Rewrap = require('./core/Main')
import * as vscode from 'vscode'
import {commands, window} from 'vscode'
import {DocState, maybeChangeWrappingColumn, rewrap, saveDocState} from './Core'
import {applyEdit, catchErr, docType, docLine} from './Common'
import {TextEditor, commands, window} from 'vscode'
import {getCoreSettings} from './Settings'
import AutoWrap from './AutoWrap'
import fixSelections from './FixSelections'


/** Function to activate the extension. */
Expand All @@ -23,7 +20,7 @@ exports.activate = async function activate(context)
/** Standard rewrap command */
function rewrapCommentCommand(editor)
{
doWrap(editor).then(() => Rewrap.saveDocState(getDocState(editor)))
doWrap(editor).then(() => saveDocState(getDocState(editor)))
}

let customWrappingColumn = 0;
Expand All @@ -44,26 +41,13 @@ exports.activate = async function activate(context)
}
}

/** Converts a selection-like object to a rewrap Selection object */
const rewrapSelection = s =>
new Selection
( new Position(s.anchor.line, s.anchor.character)
, new Position(s.active.line, s.active.character)
)

/** Gets an object representing the state of the document and selections. When a
* standard wrap is done, the state is compared with the state after the last
* wrap. If they are equal, and there are multiple rulers for the document, the
* next ruler is used for wrapping instead. */
const getDocState = editor => {
const doc = editor.document
// Conversion of selections is needed for equality operations within Fable
// code
return new DocState
( docType(doc).path
, doc.version
, editor.selections.map(rewrapSelection)
)
const getDocState = (editor: TextEditor) : DocState => {
const doc = editor.document, selections = editor.selections
return {filePath: docType(doc).path, version: doc.version, selections}
}

/** Collects the information for a wrap from the editor, passes it to the
Expand All @@ -76,11 +60,11 @@ const doWrap = (editor, customColumn?) => {
try {
const docState = getDocState(editor)
const toCol = cs => !isNaN(customColumn) ?
customColumn : Rewrap.maybeChangeWrappingColumn(docState, cs)
customColumn : maybeChangeWrappingColumn(docState, cs)
let settings = getCoreSettings(editor, toCol)
const selections = editor.selections.map(rewrapSelection)
const selections = editor.selections

const edit = Rewrap.rewrap(docType(doc), settings, selections, docLine(doc))
const edit = rewrap(docType(doc), settings, selections, docLine(doc))
return applyEdit(editor, edit).then(null, catchErr)
}
catch(err) { catchErr(err) }
Expand Down
7 changes: 0 additions & 7 deletions vscode/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ export interface AutoWrapSettings
export interface Setting<T>
{ name: string; value: T; origin: {scope: number, language: string} }

export interface CoreSettings {
column: number
tabWidth: number
doubleSentenceSpacing: boolean
reformat: boolean
wholeComment: boolean
}

/** Gets and validates a settings object from vscode's configuration. Doing this
* is not normally expensive. */
Expand Down

0 comments on commit 2ee936f

Please sign in to comment.