Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
48
Indexable
func fullDocumentContext() -> String {
        let textDocument = self.textDocumentProxy

        var before = textDocument.documentContextBeforeInput

        var completePriorString = ""

        // Grab everything before the cursor
        while (before != nil && !before!.isEmpty) {
            completePriorString = before! + completePriorString

            let length = before!.lengthOfBytes(using: .utf8)

            textDocument.adjustTextPosition(byCharacterOffset: length)
            Thread.sleep(forTimeInterval: 0.01)
            before = textDocument.documentContextBeforeInput
        }

        // Move the cursor back to the original position
        textDocument.adjustTextPosition(byCharacterOffset: completePriorString.count)
        Thread.sleep(forTimeInterval: 0.01)

        var after = textDocument.documentContextAfterInput

        var completeAfterString = ""

        // Grab everything after the cursor
        while (after != nil && !after!.isEmpty) {
            completeAfterString += after!

            let length = after!.lengthOfBytes(using: .utf8)

            textDocument.adjustTextPosition(byCharacterOffset: length)
            Thread.sleep(forTimeInterval: 0.01)
            after = textDocument.documentContextAfterInput
        }

        // Go back to the original cursor position
        textDocument.adjustTextPosition(byCharacterOffset: -(completeAfterString.count))

        let completeString = completePriorString + completeAfterString

        print(completeString)

        return completeString
    }

DispatchQueue(label: "com.example.test", qos: .userInitiated, attributes: .concurrent).async {
            let string = self.fullDocumentContext()
            debugPrint("Whole Document String",string)
        }
Editor is loading...
Leave a Comment